]>
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" | |
3aa8e4ea | 42 | #include "wx/module.h" |
39bc0347 | 43 | #include "wx/private/stattext.h" |
3aa8e4ea | 44 | #include "wx/bookctrl.h" |
52519733 | 45 | #include "wx/scrolwin.h" |
897b24cf | 46 | |
3aa8e4ea JS |
47 | #if wxUSE_DISPLAY |
48 | #include "wx/display.h" | |
49 | #endif | |
5d1b4919 VZ |
50 | |
51 | // ---------------------------------------------------------------------------- | |
92afa2b1 | 52 | // wxDialogBase |
5d1b4919 | 53 | // ---------------------------------------------------------------------------- |
e37feda2 | 54 | |
7d9f12f3 | 55 | BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow) |
a9f620da | 56 | EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton) |
2158f4d7 VZ |
57 | |
58 | EVT_CLOSE(wxDialogBase::OnCloseWindow) | |
59 | ||
0be27418 | 60 | EVT_CHAR_HOOK(wxDialogBase::OnCharHook) |
7d9f12f3 VS |
61 | END_EVENT_TABLE() |
62 | ||
3aa8e4ea JS |
63 | wxDialogLayoutAdapter* wxDialogBase::sm_layoutAdapter = NULL; |
64 | bool wxDialogBase::sm_layoutAdaptation = false; | |
65 | ||
7d9f12f3 VS |
66 | void wxDialogBase::Init() |
67 | { | |
68 | m_returnCode = 0; | |
9ceeecb9 | 69 | m_affirmativeId = wxID_OK; |
c6ece595 | 70 | m_escapeId = wxID_ANY; |
3aa8e4ea JS |
71 | m_layoutAdaptationLevel = 3; |
72 | m_layoutAdaptationDone = FALSE; | |
73 | m_layoutAdaptationMode = wxDIALOG_ADAPTATION_MODE_DEFAULT; | |
c6ece595 | 74 | |
e4b713a2 VZ |
75 | // the dialogs have this flag on by default to prevent the events from the |
76 | // dialog controls from reaching the parent frame which is usually | |
77 | // undesirable and can lead to unexpected and hard to find bugs | |
78 | SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS); | |
7d9f12f3 VS |
79 | } |
80 | ||
893f7840 VZ |
81 | // helper of GetParentForModalDialog() |
82 | static bool CanBeUsedAsParent(wxWindow *parent) | |
83 | { | |
84 | extern WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete; | |
85 | ||
86 | return !parent->HasExtraStyle(wxWS_EX_TRANSIENT) && | |
87 | parent->IsShownOnScreen() && | |
88 | !wxPendingDelete.Member(parent) && | |
9dcf65d3 | 89 | !parent->IsBeingDeleted(); |
893f7840 VZ |
90 | } |
91 | ||
2229243b VZ |
92 | wxWindow *wxDialogBase::GetParentForModalDialog(wxWindow *parent) const |
93 | { | |
94 | // creating a parent-less modal dialog will result (under e.g. wxGTK2) | |
95 | // in an unfocused dialog, so try to find a valid parent for it: | |
96 | if ( parent ) | |
97 | parent = wxGetTopLevelParent(parent); | |
98 | ||
893f7840 | 99 | if ( !parent || !CanBeUsedAsParent(parent) ) |
2229243b VZ |
100 | parent = wxTheApp->GetTopWindow(); |
101 | ||
893f7840 | 102 | if ( parent && !CanBeUsedAsParent(parent) ) |
2229243b VZ |
103 | { |
104 | // can't use this one, it's going to disappear | |
105 | parent = NULL; | |
106 | } | |
107 | ||
108 | return parent; | |
109 | } | |
110 | ||
5d1b4919 | 111 | #if wxUSE_STATTEXT |
1e6feb95 | 112 | |
cfd1ac21 MW |
113 | class wxTextSizerWrapper : public wxTextWrapper |
114 | { | |
115 | public: | |
116 | wxTextSizerWrapper(wxWindow *win) | |
117 | { | |
118 | m_win = win; | |
119 | m_hLine = 0; | |
120 | } | |
121 | ||
122 | wxSizer *CreateSizer(const wxString& text, int widthMax) | |
123 | { | |
124 | m_sizer = new wxBoxSizer(wxVERTICAL); | |
125 | Wrap(m_win, text, widthMax); | |
126 | return m_sizer; | |
127 | } | |
128 | ||
129 | protected: | |
130 | virtual void OnOutputLine(const wxString& line) | |
131 | { | |
132 | if ( !line.empty() ) | |
133 | { | |
134 | m_sizer->Add(new wxStaticText(m_win, wxID_ANY, line)); | |
135 | } | |
136 | else // empty line, no need to create a control for it | |
137 | { | |
138 | if ( !m_hLine ) | |
139 | m_hLine = m_win->GetCharHeight(); | |
140 | ||
141 | m_sizer->Add(5, m_hLine); | |
142 | } | |
143 | } | |
144 | ||
145 | private: | |
146 | wxWindow *m_win; | |
147 | wxSizer *m_sizer; | |
148 | int m_hLine; | |
149 | }; | |
150 | ||
5d1b4919 VZ |
151 | wxSizer *wxDialogBase::CreateTextSizer(const wxString& message) |
152 | { | |
2b5f62a0 VZ |
153 | // I admit that this is complete bogus, but it makes |
154 | // message boxes work for pda screens temporarily.. | |
5d1b4919 VZ |
155 | int widthMax = -1; |
156 | const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA; | |
2b5f62a0 VZ |
157 | if (is_pda) |
158 | { | |
5d1b4919 | 159 | widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25; |
2b5f62a0 | 160 | } |
68379eaf | 161 | |
5d1b4919 VZ |
162 | // '&' is used as accel mnemonic prefix in the wxWidgets controls but in |
163 | // the static messages created by CreateTextSizer() (used by wxMessageBox, | |
164 | // for example), we don't want this special meaning, so we need to quote it | |
165 | wxString text(message); | |
166 | text.Replace(_T("&"), _T("&&")); | |
68379eaf | 167 | |
cfd1ac21 | 168 | wxTextSizerWrapper wrapper(this); |
b730516c | 169 | |
cfd1ac21 MW |
170 | return wrapper.CreateSizer(text, widthMax); |
171 | } | |
5d1b4919 | 172 | |
5d1b4919 | 173 | #endif // wxUSE_STATTEXT |
1e6feb95 | 174 | |
25eb10d2 | 175 | wxSizer *wxDialogBase::CreateButtonSizer(long flags) |
e37feda2 | 176 | { |
25eb10d2 | 177 | wxSizer *sizer = NULL; |
897b24cf | 178 | |
25eb10d2 | 179 | #ifdef __SMARTPHONE__ |
102c0454 | 180 | wxDialog* dialog = (wxDialog*) this; |
25eb10d2 | 181 | if ( flags & wxOK ) |
102c0454 | 182 | dialog->SetLeftMenu(wxID_OK); |
102c0454 | 183 | |
25eb10d2 | 184 | if ( flags & wxCANCEL ) |
102c0454 | 185 | dialog->SetRightMenu(wxID_CANCEL); |
102c0454 | 186 | |
25eb10d2 | 187 | if ( flags & wxYES ) |
102c0454 | 188 | dialog->SetLeftMenu(wxID_YES); |
897b24cf | 189 | |
25eb10d2 VZ |
190 | if ( flags & wxNO ) |
191 | dialog->SetRightMenu(wxID_NO); | |
897b24cf WS |
192 | #else // !__SMARTPHONE__ |
193 | ||
25eb10d2 VZ |
194 | #if wxUSE_BUTTON |
195 | ||
897b24cf | 196 | #ifdef __POCKETPC__ |
25eb10d2 VZ |
197 | // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button |
198 | // located inside caption bar and implement Cancel functionality through | |
199 | // Undo outside dialog. As native behaviour this will be default here but | |
200 | // can be replaced with real wxButtons by setting the option below to 1 | |
201 | if ( (flags & ~(wxCANCEL|wxNO_DEFAULT)) != wxOK || | |
202 | wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) ) | |
203 | #endif // __POCKETPC__ | |
897b24cf | 204 | { |
25eb10d2 | 205 | sizer = CreateStdDialogButtonSizer(flags); |
897b24cf | 206 | } |
8d22935d VZ |
207 | #else // !wxUSE_BUTTON |
208 | wxUnusedVar(flags); | |
209 | #endif // wxUSE_BUTTON/!wxUSE_BUTTON | |
897b24cf | 210 | |
25eb10d2 | 211 | #endif // __SMARTPHONE__/!__SMARTPHONE__ |
897b24cf | 212 | |
25eb10d2 VZ |
213 | return sizer; |
214 | } | |
897b24cf | 215 | |
25eb10d2 VZ |
216 | wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags) |
217 | { | |
218 | wxSizer *sizer = CreateButtonSizer(flags); | |
219 | if ( !sizer ) | |
220 | return NULL; | |
897b24cf | 221 | |
25eb10d2 VZ |
222 | // Mac Human Interface Guidelines recommend not to use static lines as |
223 | // grouping elements | |
224 | #if wxUSE_STATLINE && !defined(__WXMAC__) | |
225 | wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL); | |
226 | topsizer->Add(new wxStaticLine(this), | |
227 | wxSizerFlags().Expand().DoubleBorder(wxBOTTOM)); | |
228 | topsizer->Add(sizer, wxSizerFlags().Expand()); | |
229 | sizer = topsizer; | |
230 | #endif // wxUSE_STATLINE | |
897b24cf | 231 | |
897b24cf | 232 | return sizer; |
acf2ac37 | 233 | } |
68379eaf | 234 | |
897b24cf WS |
235 | #if wxUSE_BUTTON |
236 | ||
acf2ac37 RR |
237 | wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags ) |
238 | { | |
239 | wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer(); | |
897b24cf | 240 | |
acf2ac37 | 241 | wxButton *ok = NULL; |
acf2ac37 RR |
242 | wxButton *yes = NULL; |
243 | wxButton *no = NULL; | |
52069700 | 244 | |
25eb10d2 VZ |
245 | if (flags & wxOK) |
246 | { | |
331c1816 | 247 | ok = new wxButton(this, wxID_OK); |
52069700 | 248 | sizer->AddButton(ok); |
2b5f62a0 | 249 | } |
52069700 | 250 | |
25eb10d2 VZ |
251 | if (flags & wxCANCEL) |
252 | { | |
331c1816 | 253 | wxButton *cancel = new wxButton(this, wxID_CANCEL); |
52069700 | 254 | sizer->AddButton(cancel); |
b5b49e42 | 255 | } |
52069700 | 256 | |
25eb10d2 VZ |
257 | if (flags & wxYES) |
258 | { | |
331c1816 | 259 | yes = new wxButton(this, wxID_YES); |
52069700 | 260 | sizer->AddButton(yes); |
e37feda2 | 261 | } |
52069700 | 262 | |
25eb10d2 VZ |
263 | if (flags & wxNO) |
264 | { | |
331c1816 | 265 | no = new wxButton(this, wxID_NO); |
52069700 | 266 | sizer->AddButton(no); |
e37feda2 | 267 | } |
52069700 | 268 | |
57d7f988 VZ |
269 | if (flags & wxAPPLY) |
270 | { | |
271 | wxButton *apply = new wxButton(this, wxID_APPLY); | |
272 | sizer->AddButton(apply); | |
273 | } | |
274 | ||
275 | if (flags & wxCLOSE) | |
276 | { | |
277 | wxButton *close = new wxButton(this, wxID_CLOSE); | |
278 | sizer->AddButton(close); | |
279 | } | |
280 | ||
25eb10d2 VZ |
281 | if (flags & wxHELP) |
282 | { | |
331c1816 | 283 | wxButton *help = new wxButton(this, wxID_HELP); |
52069700 | 284 | sizer->AddButton(help); |
e37feda2 | 285 | } |
52069700 | 286 | |
b730516c RD |
287 | if (flags & wxNO_DEFAULT) |
288 | { | |
289 | if (no) | |
290 | { | |
291 | no->SetDefault(); | |
292 | no->SetFocus(); | |
293 | } | |
294 | } | |
295 | else | |
92afa2b1 RR |
296 | { |
297 | if (ok) | |
298 | { | |
299 | ok->SetDefault(); | |
300 | ok->SetFocus(); | |
301 | } | |
302 | else if (yes) | |
303 | { | |
304 | yes->SetDefault(); | |
305 | yes->SetFocus(); | |
306 | } | |
307 | } | |
d30814cd | 308 | |
9ceeecb9 JS |
309 | if (flags & wxOK) |
310 | SetAffirmativeId(wxID_OK); | |
311 | else if (flags & wxYES) | |
312 | SetAffirmativeId(wxID_YES); | |
b730516c | 313 | |
d30814cd MB |
314 | sizer->Realize(); |
315 | ||
acf2ac37 | 316 | return sizer; |
e37feda2 VZ |
317 | } |
318 | ||
1e6feb95 | 319 | #endif // wxUSE_BUTTON |
0be27418 | 320 | |
551f281b | 321 | // ---------------------------------------------------------------------------- |
a9f620da | 322 | // standard buttons handling |
551f281b VZ |
323 | // ---------------------------------------------------------------------------- |
324 | ||
a9f620da VZ |
325 | void wxDialogBase::EndDialog(int rc) |
326 | { | |
327 | if ( IsModal() ) | |
328 | EndModal(rc); | |
329 | else | |
330 | Hide(); | |
331 | } | |
332 | ||
551f281b VZ |
333 | void wxDialogBase::AcceptAndClose() |
334 | { | |
335 | if ( Validate() && TransferDataFromWindow() ) | |
336 | { | |
a9f620da | 337 | EndDialog(m_affirmativeId); |
551f281b VZ |
338 | } |
339 | } | |
340 | ||
341 | void wxDialogBase::SetAffirmativeId(int affirmativeId) | |
342 | { | |
fabd7a7f | 343 | m_affirmativeId = affirmativeId; |
551f281b VZ |
344 | } |
345 | ||
346 | void wxDialogBase::SetEscapeId(int escapeId) | |
347 | { | |
fabd7a7f | 348 | m_escapeId = escapeId; |
551f281b VZ |
349 | } |
350 | ||
0be27418 VZ |
351 | bool wxDialogBase::EmulateButtonClickIfPresent(int id) |
352 | { | |
dbfa7f33 | 353 | #if wxUSE_BUTTON |
0be27418 VZ |
354 | wxButton *btn = wxDynamicCast(FindWindow(id), wxButton); |
355 | ||
356 | if ( !btn || !btn->IsEnabled() || !btn->IsShown() ) | |
357 | return false; | |
358 | ||
359 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, id); | |
360 | event.SetEventObject(btn); | |
361 | btn->GetEventHandler()->ProcessEvent(event); | |
362 | ||
363 | return true; | |
dbfa7f33 VS |
364 | #else // !wxUSE_BUTTON |
365 | wxUnusedVar(id); | |
366 | return false; | |
367 | #endif // wxUSE_BUTTON/!wxUSE_BUTTON | |
0be27418 VZ |
368 | } |
369 | ||
370 | bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event) | |
371 | { | |
372 | // for most platforms, Esc key is used to close the dialogs | |
373 | return event.GetKeyCode() == WXK_ESCAPE && | |
374 | event.GetModifiers() == wxMOD_NONE; | |
375 | } | |
376 | ||
377 | void wxDialogBase::OnCharHook(wxKeyEvent& event) | |
378 | { | |
379 | if ( event.GetKeyCode() == WXK_ESCAPE ) | |
380 | { | |
381 | int idCancel = GetEscapeId(); | |
382 | switch ( idCancel ) | |
383 | { | |
384 | case wxID_NONE: | |
385 | // don't handle Esc specially at all | |
386 | break; | |
387 | ||
388 | case wxID_ANY: | |
389 | // this value is special: it means translate Esc to wxID_CANCEL | |
390 | // but if there is no such button, then fall back to wxID_OK | |
391 | if ( EmulateButtonClickIfPresent(wxID_CANCEL) ) | |
392 | return; | |
153a0b78 | 393 | idCancel = GetAffirmativeId(); |
0be27418 VZ |
394 | // fall through |
395 | ||
396 | default: | |
397 | // translate Esc to button press for the button with given id | |
398 | if ( EmulateButtonClickIfPresent(idCancel) ) | |
399 | return; | |
400 | } | |
401 | } | |
402 | ||
403 | event.Skip(); | |
404 | } | |
405 | ||
a9f620da | 406 | void wxDialogBase::OnButton(wxCommandEvent& event) |
2158f4d7 | 407 | { |
a9f620da VZ |
408 | const int id = event.GetId(); |
409 | if ( id == GetAffirmativeId() ) | |
410 | { | |
411 | AcceptAndClose(); | |
412 | } | |
413 | else if ( id == wxID_APPLY ) | |
414 | { | |
415 | if ( Validate() ) | |
416 | TransferDataFromWindow(); | |
2158f4d7 | 417 | |
a9f620da VZ |
418 | // TODO: disable the Apply button until things change again |
419 | } | |
420 | else if ( id == GetEscapeId() || | |
421 | (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) ) | |
422 | { | |
423 | EndDialog(wxID_CANCEL); | |
424 | } | |
425 | else // not a standard button | |
426 | { | |
427 | event.Skip(); | |
428 | } | |
2158f4d7 VZ |
429 | } |
430 | ||
a9f620da VZ |
431 | // ---------------------------------------------------------------------------- |
432 | // other event handlers | |
433 | // ---------------------------------------------------------------------------- | |
2158f4d7 VZ |
434 | |
435 | void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
436 | { | |
437 | // We'll send a Cancel message by default, which may close the dialog. | |
438 | // Check for looping if the Cancel event handler calls Close(). | |
439 | ||
440 | // Note that if a cancel button and handler aren't present in the dialog, | |
441 | // nothing will happen when you close the dialog via the window manager, or | |
442 | // via Close(). We wouldn't want to destroy the dialog by default, since | |
443 | // the dialog may have been created on the stack. However, this does mean | |
444 | // that calling dialog->Close() won't delete the dialog unless the handler | |
445 | // for wxID_CANCEL does so. So use Destroy() if you want to be sure to | |
446 | // destroy the dialog. The default OnCancel (above) simply ends a modal | |
447 | // dialog, and hides a modeless dialog. | |
448 | ||
449 | // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global | |
450 | // lists here? don't dare to change it now, but should be done later! | |
451 | static wxList closing; | |
452 | ||
453 | if ( closing.Member(this) ) | |
454 | return; | |
455 | ||
456 | closing.Append(this); | |
457 | ||
458 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL); | |
459 | cancelEvent.SetEventObject( this ); | |
460 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
461 | ||
462 | closing.DeleteObject(this); | |
463 | } | |
464 | ||
a45f904d | 465 | void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& event) |
2158f4d7 | 466 | { |
a45f904d JS |
467 | #ifndef __WXGTK__ |
468 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
469 | Refresh(); | |
470 | #endif | |
471 | ||
472 | event.Skip(); | |
2158f4d7 | 473 | } |
3aa8e4ea JS |
474 | |
475 | /// Do the adaptation | |
476 | bool wxDialogBase::DoLayoutAdaptation() | |
477 | { | |
478 | if (GetLayoutAdapter()) | |
479 | return GetLayoutAdapter()->DoLayoutAdaptation((wxDialog*) this); | |
480 | else | |
481 | return false; | |
482 | } | |
483 | ||
484 | /// Can we do the adaptation? | |
485 | bool wxDialogBase::CanDoLayoutAdaptation() | |
486 | { | |
487 | // Check if local setting overrides the global setting | |
488 | bool layoutEnabled = (GetLayoutAdaptationMode() == wxDIALOG_ADAPTATION_MODE_ENABLED) || (IsLayoutAdaptationEnabled() && (GetLayoutAdaptationMode() != wxDIALOG_ADAPTATION_MODE_DISABLED)); | |
489 | ||
490 | return (layoutEnabled && !m_layoutAdaptationDone && GetLayoutAdaptationLevel() != 0 && GetLayoutAdapter() != NULL && GetLayoutAdapter()->CanDoLayoutAdaptation((wxDialog*) this)); | |
491 | } | |
492 | ||
493 | /// Set scrolling adapter class, returning old adapter | |
494 | wxDialogLayoutAdapter* wxDialogBase::SetLayoutAdapter(wxDialogLayoutAdapter* adapter) | |
495 | { | |
496 | wxDialogLayoutAdapter* oldLayoutAdapter = sm_layoutAdapter; | |
497 | sm_layoutAdapter = adapter; | |
498 | return oldLayoutAdapter; | |
499 | } | |
500 | ||
501 | /*! | |
502 | * Standard adapter | |
503 | */ | |
504 | ||
505 | IMPLEMENT_CLASS(wxDialogLayoutAdapter, wxObject) | |
506 | ||
507 | IMPLEMENT_CLASS(wxStandardDialogLayoutAdapter, wxDialogLayoutAdapter) | |
508 | ||
509 | // Allow for caption size on wxWidgets < 2.9 | |
510 | #if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0) | |
511 | #define wxEXTRA_DIALOG_HEIGHT 30 | |
512 | #else | |
513 | #define wxEXTRA_DIALOG_HEIGHT 0 | |
514 | #endif | |
515 | ||
516 | /// Indicate that adaptation should be done | |
517 | bool wxStandardDialogLayoutAdapter::CanDoLayoutAdaptation(wxDialog* dialog) | |
518 | { | |
519 | if (dialog->GetSizer()) | |
520 | { | |
521 | wxSize windowSize, displaySize; | |
522 | return MustScroll(dialog, windowSize, displaySize) != 0; | |
523 | } | |
524 | else | |
525 | return false; | |
526 | } | |
527 | ||
528 | bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog* dialog) | |
529 | { | |
530 | if (dialog->GetSizer()) | |
531 | { | |
f8b3f036 | 532 | #if wxUSE_BOOKCTRL |
3aa8e4ea JS |
533 | wxBookCtrlBase* bookContentWindow = wxDynamicCast(dialog->GetContentWindow(), wxBookCtrlBase); |
534 | ||
535 | if (bookContentWindow) | |
536 | { | |
537 | // If we have a book control, make all the pages (that use sizers) scrollable | |
538 | wxWindowList windows; | |
539 | for (size_t i = 0; i < bookContentWindow->GetPageCount(); i++) | |
540 | { | |
541 | wxWindow* page = bookContentWindow->GetPage(i); | |
542 | ||
543 | wxScrolledWindow* scrolledWindow = wxDynamicCast(page, wxScrolledWindow); | |
544 | if (scrolledWindow) | |
545 | windows.Append(scrolledWindow); | |
546 | else if (!scrolledWindow && page->GetSizer()) | |
547 | { | |
548 | // Create a scrolled window and reparent | |
549 | scrolledWindow = CreateScrolledWindow(page); | |
550 | wxSizer* oldSizer = page->GetSizer(); | |
551 | ||
552 | wxSizer* newSizer = new wxBoxSizer(wxVERTICAL); | |
553 | newSizer->Add(scrolledWindow,1, wxEXPAND, 0); | |
554 | ||
555 | page->SetSizer(newSizer, false /* don't delete the old sizer */); | |
556 | ||
557 | scrolledWindow->SetSizer(oldSizer); | |
558 | ||
559 | ReparentControls(page, scrolledWindow); | |
560 | ||
561 | windows.Append(scrolledWindow); | |
562 | } | |
563 | } | |
564 | ||
565 | FitWithScrolling(dialog, windows); | |
566 | } | |
567 | else | |
f8b3f036 | 568 | #endif // wxUSE_BOOKCTRL |
3aa8e4ea JS |
569 | { |
570 | // If we have an arbitrary dialog, create a scrolling area for the main content, and a button sizer | |
571 | // for the main buttons. | |
572 | wxScrolledWindow* scrolledWindow = CreateScrolledWindow(dialog); | |
573 | ||
574 | int buttonSizerBorder = 0; | |
575 | ||
576 | // First try to find a wxStdDialogButtonSizer | |
577 | wxSizer* buttonSizer = FindButtonSizer(true /* find std button sizer */, dialog, dialog->GetSizer(), buttonSizerBorder); | |
578 | ||
579 | // Next try to find a wxBoxSizer containing the controls | |
580 | if (!buttonSizer && dialog->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_STANDARD_SIZER) | |
581 | buttonSizer = FindButtonSizer(false /* find ordinary sizer */, dialog, dialog->GetSizer(), buttonSizerBorder); | |
582 | ||
583 | // If we still don't have a button sizer, collect any 'loose' buttons in the layout | |
584 | if (!buttonSizer && dialog->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_ANY_SIZER) | |
585 | { | |
586 | int count = 0; | |
587 | wxStdDialogButtonSizer* stdButtonSizer = new wxStdDialogButtonSizer; | |
588 | buttonSizer = stdButtonSizer; | |
589 | ||
590 | FindLooseButtons(dialog, stdButtonSizer, dialog->GetSizer(), count); | |
591 | if (count > 0) | |
592 | stdButtonSizer->Realize(); | |
593 | else | |
594 | { | |
595 | delete buttonSizer; | |
596 | buttonSizer = NULL; | |
597 | } | |
598 | } | |
599 | ||
600 | if (buttonSizerBorder == 0) | |
601 | buttonSizerBorder = 5; | |
602 | ||
603 | ReparentControls(dialog, scrolledWindow, buttonSizer); | |
604 | ||
605 | wxBoxSizer* newTopSizer = new wxBoxSizer(wxVERTICAL); | |
606 | wxSizer* oldSizer = dialog->GetSizer(); | |
607 | ||
608 | dialog->SetSizer(newTopSizer, false /* don't delete old sizer */); | |
609 | ||
610 | newTopSizer->Add(scrolledWindow, 1, wxEXPAND|wxALL, 0); | |
611 | if (buttonSizer) | |
612 | newTopSizer->Add(buttonSizer, 0, wxEXPAND|wxALL, buttonSizerBorder); | |
613 | ||
614 | scrolledWindow->SetSizer(oldSizer); | |
615 | ||
616 | FitWithScrolling(dialog, scrolledWindow); | |
617 | } | |
618 | } | |
619 | ||
620 | dialog->SetLayoutAdaptationDone(true); | |
621 | return true; | |
622 | } | |
623 | ||
624 | // Create the scrolled window | |
625 | wxScrolledWindow* wxStandardDialogLayoutAdapter::CreateScrolledWindow(wxWindow* parent) | |
626 | { | |
627 | wxScrolledWindow* scrolledWindow = new wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxVSCROLL|wxHSCROLL|wxBORDER_NONE); | |
628 | return scrolledWindow; | |
629 | } | |
630 | ||
631 | /// Find and remove the button sizer, if any | |
632 | wxSizer* wxStandardDialogLayoutAdapter::FindButtonSizer(bool stdButtonSizer, wxDialog* dialog, wxSizer* sizer, int& retBorder, int accumlatedBorder) | |
633 | { | |
634 | for ( wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst(); | |
635 | node; node = node->GetNext() ) | |
636 | { | |
637 | wxSizerItem *item = node->GetData(); | |
638 | wxSizer *childSizer = item->GetSizer(); | |
639 | ||
640 | if ( childSizer ) | |
641 | { | |
642 | int newBorder = accumlatedBorder; | |
643 | if (item->GetFlag() & wxALL) | |
644 | newBorder += item->GetBorder(); | |
645 | ||
646 | if (stdButtonSizer) // find wxStdDialogButtonSizer | |
647 | { | |
648 | wxStdDialogButtonSizer* buttonSizer = wxDynamicCast(childSizer, wxStdDialogButtonSizer); | |
649 | if (buttonSizer) | |
650 | { | |
651 | sizer->Detach(childSizer); | |
652 | retBorder = newBorder; | |
653 | return buttonSizer; | |
654 | } | |
655 | } | |
656 | else // find a horizontal box sizer containing standard buttons | |
657 | { | |
658 | wxBoxSizer* buttonSizer = wxDynamicCast(childSizer, wxBoxSizer); | |
659 | if (buttonSizer && IsOrdinaryButtonSizer(dialog, buttonSizer)) | |
660 | { | |
661 | sizer->Detach(childSizer); | |
662 | retBorder = newBorder; | |
663 | return buttonSizer; | |
664 | } | |
665 | } | |
666 | ||
667 | wxSizer* s = FindButtonSizer(stdButtonSizer, dialog, childSizer, retBorder, newBorder); | |
668 | if (s) | |
669 | return s; | |
670 | } | |
671 | } | |
672 | return NULL; | |
673 | } | |
674 | ||
675 | /// Check if this sizer contains standard buttons, and so can be repositioned in the dialog | |
676 | bool wxStandardDialogLayoutAdapter::IsOrdinaryButtonSizer(wxDialog* dialog, wxBoxSizer* sizer) | |
677 | { | |
678 | if (sizer->GetOrientation() != wxHORIZONTAL) | |
679 | return false; | |
680 | ||
681 | for ( wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst(); | |
682 | node; node = node->GetNext() ) | |
683 | { | |
684 | wxSizerItem *item = node->GetData(); | |
685 | wxButton *childButton = wxDynamicCast(item->GetWindow(), wxButton); | |
686 | ||
687 | if (childButton && IsStandardButton(dialog, childButton)) | |
688 | return true; | |
689 | } | |
690 | return false; | |
691 | } | |
692 | ||
693 | /// Check if this is a standard button | |
694 | bool wxStandardDialogLayoutAdapter::IsStandardButton(wxDialog* dialog, wxButton* button) | |
695 | { | |
696 | wxWindowID id = button->GetId(); | |
697 | ||
698 | return (id == wxID_OK || id == wxID_CANCEL || id == wxID_YES || id == wxID_NO || id == wxID_SAVE || | |
699 | id == wxID_APPLY || id == wxID_HELP || id == wxID_CONTEXT_HELP || dialog->IsMainButtonId(id)); | |
700 | } | |
701 | ||
702 | /// Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer | |
703 | bool wxStandardDialogLayoutAdapter::FindLooseButtons(wxDialog* dialog, wxStdDialogButtonSizer* buttonSizer, wxSizer* sizer, int& count) | |
704 | { | |
705 | wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst(); | |
706 | while (node) | |
707 | { | |
708 | wxSizerItemList::compatibility_iterator next = node->GetNext(); | |
709 | wxSizerItem *item = node->GetData(); | |
710 | wxSizer *childSizer = item->GetSizer(); | |
711 | wxButton *childButton = wxDynamicCast(item->GetWindow(), wxButton); | |
712 | ||
713 | if (childButton && IsStandardButton(dialog, childButton)) | |
714 | { | |
715 | sizer->Detach(childButton); | |
716 | buttonSizer->AddButton(childButton); | |
717 | count ++; | |
718 | } | |
719 | ||
720 | if (childSizer) | |
721 | FindLooseButtons(dialog, buttonSizer, childSizer, count); | |
722 | ||
723 | node = next; | |
724 | } | |
725 | return true; | |
726 | } | |
727 | ||
728 | /// Reparent the controls to the scrolled window | |
729 | void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer) | |
730 | { | |
731 | DoReparentControls(parent, reparentTo, buttonSizer); | |
732 | } | |
733 | ||
734 | void wxStandardDialogLayoutAdapter::DoReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer) | |
735 | { | |
736 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst(); | |
737 | while (node) | |
738 | { | |
739 | wxWindowList::compatibility_iterator next = node->GetNext(); | |
740 | ||
741 | wxWindow *win = node->GetData(); | |
742 | ||
743 | // Don't reparent the scrolled window or buttons in the button sizer | |
744 | if (win != reparentTo && (!buttonSizer || !buttonSizer->GetItem(win))) | |
745 | { | |
746 | win->Reparent(reparentTo); | |
747 | #ifdef __WXMSW__ | |
748 | // Restore correct tab order | |
749 | ::SetWindowPos((HWND) win->GetHWND(), HWND_BOTTOM, -1, -1, -1, -1, SWP_NOMOVE|SWP_NOSIZE); | |
750 | #endif | |
751 | } | |
752 | ||
753 | node = next; | |
754 | } | |
755 | } | |
756 | ||
757 | /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both | |
758 | int wxStandardDialogLayoutAdapter::MustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize) | |
759 | { | |
760 | return DoMustScroll(dialog, windowSize, displaySize); | |
761 | } | |
762 | ||
763 | /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both | |
764 | int wxStandardDialogLayoutAdapter::DoMustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize) | |
765 | { | |
766 | wxSize minWindowSize = dialog->GetSizer()->GetMinSize(); | |
767 | windowSize = dialog->GetSize(); | |
768 | windowSize = wxSize(wxMax(windowSize.x, minWindowSize.x), wxMax(windowSize.y, minWindowSize.y)); | |
769 | #if wxUSE_DISPLAY | |
770 | displaySize = wxDisplay(wxDisplay::GetFromWindow(dialog)).GetClientArea().GetSize(); | |
771 | #else | |
728cf0ad | 772 | displaySize = wxGetClientDisplayRect().GetSize(); |
3aa8e4ea JS |
773 | #endif |
774 | ||
775 | int flags = 0; | |
776 | ||
777 | if (windowSize.y >= (displaySize.y - wxEXTRA_DIALOG_HEIGHT)) | |
778 | flags |= wxVERTICAL; | |
779 | if (windowSize.x >= displaySize.x) | |
780 | flags |= wxHORIZONTAL; | |
781 | ||
782 | return flags; | |
783 | } | |
784 | ||
785 | // A function to fit the dialog around its contents, and then adjust for screen size. | |
786 | // If scrolled windows are passed, scrolling is enabled in the required orientation(s). | |
787 | bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog* dialog, wxWindowList& windows) | |
788 | { | |
789 | return DoFitWithScrolling(dialog, windows); | |
790 | } | |
791 | ||
792 | // A function to fit the dialog around its contents, and then adjust for screen size. | |
793 | // If a scrolled window is passed, scrolling is enabled in the required orientation(s). | |
794 | bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow) | |
795 | { | |
796 | return DoFitWithScrolling(dialog, scrolledWindow); | |
797 | } | |
798 | ||
799 | // A function to fit the dialog around its contents, and then adjust for screen size. | |
800 | // If a scrolled window is passed, scrolling is enabled in the required orientation(s). | |
801 | bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow) | |
802 | { | |
803 | wxWindowList windows; | |
804 | windows.Append(scrolledWindow); | |
805 | return DoFitWithScrolling(dialog, windows); | |
806 | } | |
807 | ||
808 | bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog* dialog, wxWindowList& windows) | |
809 | { | |
810 | wxSizer* sizer = dialog->GetSizer(); | |
811 | if (!sizer) | |
812 | return false; | |
813 | ||
814 | sizer->SetSizeHints(dialog); | |
815 | ||
816 | wxSize windowSize, displaySize; | |
817 | int scrollFlags = DoMustScroll(dialog, windowSize, displaySize); | |
818 | int scrollBarSize = 20; | |
819 | ||
820 | if (scrollFlags) | |
821 | { | |
822 | int scrollBarExtraX = 0, scrollBarExtraY = 0; | |
823 | bool resizeHorizontally = (scrollFlags & wxHORIZONTAL) != 0; | |
824 | bool resizeVertically = (scrollFlags & wxVERTICAL) != 0; | |
825 | ||
826 | if (windows.GetCount() != 0) | |
827 | { | |
828 | // Allow extra for a scrollbar, assuming we resizing in one direction only. | |
829 | if ((resizeVertically && !resizeHorizontally) && (windowSize.x < (displaySize.x - scrollBarSize))) | |
830 | scrollBarExtraX = scrollBarSize; | |
831 | if ((resizeHorizontally && !resizeVertically) && (windowSize.y < (displaySize.y - scrollBarSize))) | |
832 | scrollBarExtraY = scrollBarSize; | |
833 | } | |
834 | ||
835 | wxWindowList::compatibility_iterator node = windows.GetFirst(); | |
836 | while (node) | |
837 | { | |
838 | wxWindow *win = node->GetData(); | |
839 | wxScrolledWindow* scrolledWindow = wxDynamicCast(win, wxScrolledWindow); | |
840 | if (scrolledWindow) | |
841 | { | |
842 | scrolledWindow->SetScrollRate(resizeHorizontally ? 10 : 0, resizeVertically ? 10 : 0); | |
843 | ||
844 | if (scrolledWindow->GetSizer()) | |
845 | scrolledWindow->GetSizer()->Fit(scrolledWindow); | |
846 | } | |
847 | ||
848 | node = node->GetNext(); | |
849 | } | |
850 | ||
851 | wxSize limitTo = windowSize + wxSize(scrollBarExtraX, scrollBarExtraY); | |
852 | if (resizeVertically) | |
853 | limitTo.y = displaySize.y - wxEXTRA_DIALOG_HEIGHT; | |
854 | if (resizeHorizontally) | |
855 | limitTo.x = displaySize.x; | |
856 | ||
857 | dialog->SetMinSize(limitTo); | |
858 | dialog->SetSize(limitTo); | |
859 | ||
860 | dialog->SetSizeHints( limitTo.x, limitTo.y, dialog->GetMaxWidth(), dialog->GetMaxHeight() ); | |
861 | } | |
862 | ||
863 | return true; | |
864 | } | |
865 | ||
866 | /*! | |
867 | * Module to initialise standard adapter | |
868 | */ | |
869 | ||
870 | class wxDialogLayoutAdapterModule: public wxModule | |
871 | { | |
872 | DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule) | |
873 | public: | |
874 | wxDialogLayoutAdapterModule() {} | |
875 | virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL); } | |
876 | virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter); return true; } | |
877 | }; | |
878 | ||
879 | IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule, wxModule) |