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"
30 #include "wx/button.h"
31 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/stattext.h"
36 #include "wx/containr.h"
39 #include "wx/statline.h"
40 #include "wx/sysopt.h"
41 #include "wx/private/stattext.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
49 EVT_BUTTON(wxID_ANY
, wxDialogBase::OnButton
)
51 EVT_CLOSE(wxDialogBase::OnCloseWindow
)
53 EVT_CHAR_HOOK(wxDialogBase::OnCharHook
)
55 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
58 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
, wxTopLevelWindow
)
60 void wxDialogBase::Init()
63 m_affirmativeId
= wxID_OK
;
64 m_escapeId
= wxID_ANY
;
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
);
71 WX_INIT_CONTROL_CONTAINER();
76 class wxTextSizerWrapper
: public wxTextWrapper
79 wxTextSizerWrapper(wxWindow
*win
)
85 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
87 m_sizer
= new wxBoxSizer(wxVERTICAL
);
88 Wrap(m_win
, text
, widthMax
);
93 virtual void OnOutputLine(const wxString
& line
)
97 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
99 else // empty line, no need to create a control for it
102 m_hLine
= m_win
->GetCharHeight();
104 m_sizer
->Add(5, m_hLine
);
114 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
116 // I admit that this is complete bogus, but it makes
117 // message boxes work for pda screens temporarily..
119 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
122 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
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("&&"));
131 wxTextSizerWrapper
wrapper(this);
133 return wrapper
.CreateSizer(text
, widthMax
);
136 #endif // wxUSE_STATTEXT
138 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
140 wxSizer
*sizer
= NULL
;
142 #ifdef __SMARTPHONE__
143 wxDialog
* dialog
= (wxDialog
*) this;
145 dialog
->SetLeftMenu(wxID_OK
);
147 if ( flags
& wxCANCEL
)
148 dialog
->SetRightMenu(wxID_CANCEL
);
151 dialog
->SetLeftMenu(wxID_YES
);
154 dialog
->SetRightMenu(wxID_NO
);
155 #else // !__SMARTPHONE__
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__
168 sizer
= CreateStdDialogButtonSizer(flags
);
170 #endif // wxUSE_BUTTON
172 #endif // __SMARTPHONE__/!__SMARTPHONE__
177 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
179 wxSizer
*sizer
= CreateButtonSizer(flags
);
183 // Mac Human Interface Guidelines recommend not to use static lines as
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());
191 #endif // wxUSE_STATLINE
198 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
200 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
203 wxButton
*yes
= NULL
;
208 ok
= new wxButton(this, wxID_OK
);
209 sizer
->AddButton(ok
);
212 if (flags
& wxCANCEL
)
214 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
215 sizer
->AddButton(cancel
);
220 yes
= new wxButton(this, wxID_YES
);
221 sizer
->AddButton(yes
);
226 no
= new wxButton(this, wxID_NO
);
227 sizer
->AddButton(no
);
232 wxButton
*help
= new wxButton(this, wxID_HELP
);
233 sizer
->AddButton(help
);
236 if (flags
& wxNO_DEFAULT
)
259 SetAffirmativeId(wxID_OK
);
260 else if (flags
& wxYES
)
261 SetAffirmativeId(wxID_YES
);
268 #endif // wxUSE_BUTTON
270 // ----------------------------------------------------------------------------
271 // standard buttons handling
272 // ----------------------------------------------------------------------------
274 void wxDialogBase::EndDialog(int rc
)
282 void wxDialogBase::AcceptAndClose()
284 if ( Validate() && TransferDataFromWindow() )
286 EndDialog(m_affirmativeId
);
290 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
292 m_affirmativeId
= affirmativeId
;
295 void wxDialogBase::SetEscapeId(int escapeId
)
297 m_escapeId
= escapeId
;
300 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
303 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
305 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
308 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
309 event
.SetEventObject(btn
);
310 btn
->GetEventHandler()->ProcessEvent(event
);
313 #else // !wxUSE_BUTTON
316 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
319 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
321 // for most platforms, Esc key is used to close the dialogs
322 return event
.GetKeyCode() == WXK_ESCAPE
&&
323 event
.GetModifiers() == wxMOD_NONE
;
326 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
328 if ( event
.GetKeyCode() == WXK_ESCAPE
)
330 int idCancel
= GetEscapeId();
334 // don't handle Esc specially at all
338 // this value is special: it means translate Esc to wxID_CANCEL
339 // but if there is no such button, then fall back to wxID_OK
340 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
342 idCancel
= GetAffirmativeId();
346 // translate Esc to button press for the button with given id
347 if ( EmulateButtonClickIfPresent(idCancel
) )
355 void wxDialogBase::OnButton(wxCommandEvent
& event
)
357 const int id
= event
.GetId();
358 if ( id
== GetAffirmativeId() )
362 else if ( id
== wxID_APPLY
)
365 TransferDataFromWindow();
367 // TODO: disable the Apply button until things change again
369 else if ( id
== GetEscapeId() ||
370 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
372 EndDialog(wxID_CANCEL
);
374 else // not a standard button
380 // ----------------------------------------------------------------------------
381 // other event handlers
382 // ----------------------------------------------------------------------------
384 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
386 // We'll send a Cancel message by default, which may close the dialog.
387 // Check for looping if the Cancel event handler calls Close().
389 // Note that if a cancel button and handler aren't present in the dialog,
390 // nothing will happen when you close the dialog via the window manager, or
391 // via Close(). We wouldn't want to destroy the dialog by default, since
392 // the dialog may have been created on the stack. However, this does mean
393 // that calling dialog->Close() won't delete the dialog unless the handler
394 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
395 // destroy the dialog. The default OnCancel (above) simply ends a modal
396 // dialog, and hides a modeless dialog.
398 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
399 // lists here? don't dare to change it now, but should be done later!
400 static wxList closing
;
402 if ( closing
.Member(this) )
405 closing
.Append(this);
407 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
408 cancelEvent
.SetEventObject( this );
409 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
411 closing
.DeleteObject(this);
414 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
416 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));