1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dlgcmn.cpp
3 // Purpose: common (to all ports) wxDialog functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/dialog.h"
31 #include "wx/button.h"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/stattext.h"
37 #include "wx/containr.h"
40 #include "wx/statline.h"
41 #include "wx/sysopt.h"
42 #include "wx/private/stattext.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
50 EVT_BUTTON(wxID_ANY
, wxDialogBase::OnButton
)
52 EVT_CLOSE(wxDialogBase::OnCloseWindow
)
54 EVT_CHAR_HOOK(wxDialogBase::OnCharHook
)
57 void wxDialogBase::Init()
60 m_affirmativeId
= wxID_OK
;
61 m_escapeId
= wxID_ANY
;
63 // the dialogs have this flag on by default to prevent the events from the
64 // dialog controls from reaching the parent frame which is usually
65 // undesirable and can lead to unexpected and hard to find bugs
66 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
69 // helper of GetParentForModalDialog()
70 static bool CanBeUsedAsParent(wxWindow
*parent
)
72 extern WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
74 return !parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) &&
75 parent
->IsShownOnScreen() &&
76 !wxPendingDelete
.Member(parent
) &&
77 !parent
->IsBeingDeleted();
80 wxWindow
*wxDialogBase::GetParentForModalDialog(wxWindow
*parent
) const
82 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
83 // in an unfocused dialog, so try to find a valid parent for it:
85 parent
= wxGetTopLevelParent(parent
);
87 if ( !parent
|| !CanBeUsedAsParent(parent
) )
88 parent
= wxTheApp
->GetTopWindow();
90 if ( parent
&& !CanBeUsedAsParent(parent
) )
92 // can't use this one, it's going to disappear
101 class wxTextSizerWrapper
: public wxTextWrapper
104 wxTextSizerWrapper(wxWindow
*win
)
110 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
112 m_sizer
= new wxBoxSizer(wxVERTICAL
);
113 Wrap(m_win
, text
, widthMax
);
118 virtual void OnOutputLine(const wxString
& line
)
122 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
124 else // empty line, no need to create a control for it
127 m_hLine
= m_win
->GetCharHeight();
129 m_sizer
->Add(5, m_hLine
);
139 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
141 // I admit that this is complete bogus, but it makes
142 // message boxes work for pda screens temporarily..
144 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
147 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
150 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
151 // the static messages created by CreateTextSizer() (used by wxMessageBox,
152 // for example), we don't want this special meaning, so we need to quote it
153 wxString
text(message
);
154 text
.Replace(_T("&"), _T("&&"));
156 wxTextSizerWrapper
wrapper(this);
158 return wrapper
.CreateSizer(text
, widthMax
);
161 #endif // wxUSE_STATTEXT
163 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
165 wxSizer
*sizer
= NULL
;
167 #ifdef __SMARTPHONE__
168 wxDialog
* dialog
= (wxDialog
*) this;
170 dialog
->SetLeftMenu(wxID_OK
);
172 if ( flags
& wxCANCEL
)
173 dialog
->SetRightMenu(wxID_CANCEL
);
176 dialog
->SetLeftMenu(wxID_YES
);
179 dialog
->SetRightMenu(wxID_NO
);
180 #else // !__SMARTPHONE__
185 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
186 // located inside caption bar and implement Cancel functionality through
187 // Undo outside dialog. As native behaviour this will be default here but
188 // can be replaced with real wxButtons by setting the option below to 1
189 if ( (flags
& ~(wxCANCEL
|wxNO_DEFAULT
)) != wxOK
||
190 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
191 #endif // __POCKETPC__
193 sizer
= CreateStdDialogButtonSizer(flags
);
195 #else // !wxUSE_BUTTON
197 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
199 #endif // __SMARTPHONE__/!__SMARTPHONE__
204 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
206 wxSizer
*sizer
= CreateButtonSizer(flags
);
210 // Mac Human Interface Guidelines recommend not to use static lines as
212 #if wxUSE_STATLINE && !defined(__WXMAC__)
213 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
214 topsizer
->Add(new wxStaticLine(this),
215 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
216 topsizer
->Add(sizer
, wxSizerFlags().Expand());
218 #endif // wxUSE_STATLINE
225 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
227 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
230 wxButton
*yes
= NULL
;
235 ok
= new wxButton(this, wxID_OK
);
236 sizer
->AddButton(ok
);
239 if (flags
& wxCANCEL
)
241 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
242 sizer
->AddButton(cancel
);
247 yes
= new wxButton(this, wxID_YES
);
248 sizer
->AddButton(yes
);
253 no
= new wxButton(this, wxID_NO
);
254 sizer
->AddButton(no
);
259 wxButton
*apply
= new wxButton(this, wxID_APPLY
);
260 sizer
->AddButton(apply
);
265 wxButton
*close
= new wxButton(this, wxID_CLOSE
);
266 sizer
->AddButton(close
);
271 wxButton
*help
= new wxButton(this, wxID_HELP
);
272 sizer
->AddButton(help
);
275 if (flags
& wxNO_DEFAULT
)
298 SetAffirmativeId(wxID_OK
);
299 else if (flags
& wxYES
)
300 SetAffirmativeId(wxID_YES
);
307 #endif // wxUSE_BUTTON
309 // ----------------------------------------------------------------------------
310 // standard buttons handling
311 // ----------------------------------------------------------------------------
313 void wxDialogBase::EndDialog(int rc
)
321 void wxDialogBase::AcceptAndClose()
323 if ( Validate() && TransferDataFromWindow() )
325 EndDialog(m_affirmativeId
);
329 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
331 m_affirmativeId
= affirmativeId
;
334 void wxDialogBase::SetEscapeId(int escapeId
)
336 m_escapeId
= escapeId
;
339 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
342 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
344 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
347 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
348 event
.SetEventObject(btn
);
349 btn
->GetEventHandler()->ProcessEvent(event
);
352 #else // !wxUSE_BUTTON
355 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
358 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
360 // for most platforms, Esc key is used to close the dialogs
361 return event
.GetKeyCode() == WXK_ESCAPE
&&
362 event
.GetModifiers() == wxMOD_NONE
;
365 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
367 if ( event
.GetKeyCode() == WXK_ESCAPE
)
369 int idCancel
= GetEscapeId();
373 // don't handle Esc specially at all
377 // this value is special: it means translate Esc to wxID_CANCEL
378 // but if there is no such button, then fall back to wxID_OK
379 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
381 idCancel
= GetAffirmativeId();
385 // translate Esc to button press for the button with given id
386 if ( EmulateButtonClickIfPresent(idCancel
) )
394 void wxDialogBase::OnButton(wxCommandEvent
& event
)
396 const int id
= event
.GetId();
397 if ( id
== GetAffirmativeId() )
401 else if ( id
== wxID_APPLY
)
404 TransferDataFromWindow();
406 // TODO: disable the Apply button until things change again
408 else if ( id
== GetEscapeId() ||
409 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
411 EndDialog(wxID_CANCEL
);
413 else // not a standard button
419 // ----------------------------------------------------------------------------
420 // other event handlers
421 // ----------------------------------------------------------------------------
423 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
425 // We'll send a Cancel message by default, which may close the dialog.
426 // Check for looping if the Cancel event handler calls Close().
428 // Note that if a cancel button and handler aren't present in the dialog,
429 // nothing will happen when you close the dialog via the window manager, or
430 // via Close(). We wouldn't want to destroy the dialog by default, since
431 // the dialog may have been created on the stack. However, this does mean
432 // that calling dialog->Close() won't delete the dialog unless the handler
433 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
434 // destroy the dialog. The default OnCancel (above) simply ends a modal
435 // dialog, and hides a modeless dialog.
437 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
438 // lists here? don't dare to change it now, but should be done later!
439 static wxList closing
;
441 if ( closing
.Member(this) )
444 closing
.Append(this);
446 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
447 cancelEvent
.SetEventObject( this );
448 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
450 closing
.DeleteObject(this);
453 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
456 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));