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