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
)
56 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
59 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
, wxTopLevelWindow
)
61 void wxDialogBase
::Init()
64 m_affirmativeId
= wxID_OK
;
65 m_escapeId
= wxID_ANY
;
67 // the dialogs have this flag on by default to prevent the events from the
68 // dialog controls from reaching the parent frame which is usually
69 // undesirable and can lead to unexpected and hard to find bugs
70 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
72 WX_INIT_CONTROL_CONTAINER();
75 wxWindow
*wxDialogBase
::GetParentForModalDialog(wxWindow
*parent
) const
77 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
78 // in an unfocused dialog, so try to find a valid parent for it:
80 parent
= wxGetTopLevelParent(parent
);
82 if ( !parent
|| parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) )
83 parent
= wxTheApp
->GetTopWindow();
85 if ( parent
&& parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) )
87 // can't use this one, it's going to disappear
96 class wxTextSizerWrapper
: public wxTextWrapper
99 wxTextSizerWrapper(wxWindow
*win
)
105 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
107 m_sizer
= new wxBoxSizer(wxVERTICAL
);
108 Wrap(m_win
, text
, widthMax
);
113 virtual void OnOutputLine(const wxString
& line
)
117 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
119 else // empty line, no need to create a control for it
122 m_hLine
= m_win
->GetCharHeight();
124 m_sizer
->Add(5, m_hLine
);
134 wxSizer
*wxDialogBase
::CreateTextSizer(const wxString
& message
)
136 // I admit that this is complete bogus, but it makes
137 // message boxes work for pda screens temporarily..
139 const bool is_pda
= wxSystemSettings
::GetScreenType() <= wxSYS_SCREEN_PDA
;
142 widthMax
= wxSystemSettings
::GetMetric( wxSYS_SCREEN_X
) - 25;
145 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
146 // the static messages created by CreateTextSizer() (used by wxMessageBox,
147 // for example), we don't want this special meaning, so we need to quote it
148 wxString
text(message
);
149 text
.Replace(_T("&"), _T("&&"));
151 wxTextSizerWrapper
wrapper(this);
153 return wrapper
.CreateSizer(text
, widthMax
);
156 #endif // wxUSE_STATTEXT
158 wxSizer
*wxDialogBase
::CreateButtonSizer(long flags
)
160 wxSizer
*sizer
= NULL
;
162 #ifdef __SMARTPHONE__
163 wxDialog
* dialog
= (wxDialog
*) this;
165 dialog
->SetLeftMenu(wxID_OK
);
167 if ( flags
& wxCANCEL
)
168 dialog
->SetRightMenu(wxID_CANCEL
);
171 dialog
->SetLeftMenu(wxID_YES
);
174 dialog
->SetRightMenu(wxID_NO
);
175 #else // !__SMARTPHONE__
180 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
181 // located inside caption bar and implement Cancel functionality through
182 // Undo outside dialog. As native behaviour this will be default here but
183 // can be replaced with real wxButtons by setting the option below to 1
184 if ( (flags
& ~(wxCANCEL
|wxNO_DEFAULT
)) != wxOK
||
185 wxSystemOptions
::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
186 #endif // __POCKETPC__
188 sizer
= CreateStdDialogButtonSizer(flags
);
190 #endif // wxUSE_BUTTON
192 #endif // __SMARTPHONE__/!__SMARTPHONE__
197 wxSizer
*wxDialogBase
::CreateSeparatedButtonSizer(long flags
)
199 wxSizer
*sizer
= CreateButtonSizer(flags
);
203 // Mac Human Interface Guidelines recommend not to use static lines as
205 #if wxUSE_STATLINE && !defined(__WXMAC__)
206 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
207 topsizer
->Add(new wxStaticLine(this),
208 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
209 topsizer
->Add(sizer
, wxSizerFlags().Expand());
211 #endif // wxUSE_STATLINE
218 wxStdDialogButtonSizer
*wxDialogBase
::CreateStdDialogButtonSizer( long flags
)
220 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
223 wxButton
*yes
= NULL
;
228 ok
= new wxButton(this, wxID_OK
);
229 sizer
->AddButton(ok
);
232 if (flags
& wxCANCEL
)
234 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
235 sizer
->AddButton(cancel
);
240 yes
= new wxButton(this, wxID_YES
);
241 sizer
->AddButton(yes
);
246 no
= new wxButton(this, wxID_NO
);
247 sizer
->AddButton(no
);
252 wxButton
*apply
= new wxButton(this, wxID_APPLY
);
253 sizer
->AddButton(apply
);
258 wxButton
*close
= new wxButton(this, wxID_CLOSE
);
259 sizer
->AddButton(close
);
264 wxButton
*help
= new wxButton(this, wxID_HELP
);
265 sizer
->AddButton(help
);
268 if (flags
& wxNO_DEFAULT
)
291 SetAffirmativeId(wxID_OK
);
292 else if (flags
& wxYES
)
293 SetAffirmativeId(wxID_YES
);
300 #endif // wxUSE_BUTTON
302 // ----------------------------------------------------------------------------
303 // standard buttons handling
304 // ----------------------------------------------------------------------------
306 void wxDialogBase
::EndDialog(int rc
)
314 void wxDialogBase
::AcceptAndClose()
316 if ( Validate() && TransferDataFromWindow() )
318 EndDialog(m_affirmativeId
);
322 void wxDialogBase
::SetAffirmativeId(int affirmativeId
)
324 m_affirmativeId
= affirmativeId
;
327 void wxDialogBase
::SetEscapeId(int escapeId
)
329 m_escapeId
= escapeId
;
332 bool wxDialogBase
::EmulateButtonClickIfPresent(int id
)
335 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
337 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
340 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
341 event
.SetEventObject(btn
);
342 btn
->GetEventHandler()->ProcessEvent(event
);
345 #else // !wxUSE_BUTTON
348 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
351 bool wxDialogBase
::IsEscapeKey(const wxKeyEvent
& event
)
353 // for most platforms, Esc key is used to close the dialogs
354 return event
.GetKeyCode() == WXK_ESCAPE
&&
355 event
.GetModifiers() == wxMOD_NONE
;
358 void wxDialogBase
::OnCharHook(wxKeyEvent
& event
)
360 if ( event
.GetKeyCode() == WXK_ESCAPE
)
362 int idCancel
= GetEscapeId();
366 // don't handle Esc specially at all
370 // this value is special: it means translate Esc to wxID_CANCEL
371 // but if there is no such button, then fall back to wxID_OK
372 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
374 idCancel
= GetAffirmativeId();
378 // translate Esc to button press for the button with given id
379 if ( EmulateButtonClickIfPresent(idCancel
) )
387 void wxDialogBase
::OnButton(wxCommandEvent
& event
)
389 const int id
= event
.GetId();
390 if ( id
== GetAffirmativeId() )
394 else if ( id
== wxID_APPLY
)
397 TransferDataFromWindow();
399 // TODO: disable the Apply button until things change again
401 else if ( id
== GetEscapeId() ||
402 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
404 EndDialog(wxID_CANCEL
);
406 else // not a standard button
412 // ----------------------------------------------------------------------------
413 // other event handlers
414 // ----------------------------------------------------------------------------
416 void wxDialogBase
::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
418 // We'll send a Cancel message by default, which may close the dialog.
419 // Check for looping if the Cancel event handler calls Close().
421 // Note that if a cancel button and handler aren't present in the dialog,
422 // nothing will happen when you close the dialog via the window manager, or
423 // via Close(). We wouldn't want to destroy the dialog by default, since
424 // the dialog may have been created on the stack. However, this does mean
425 // that calling dialog->Close() won't delete the dialog unless the handler
426 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
427 // destroy the dialog. The default OnCancel (above) simply ends a modal
428 // dialog, and hides a modeless dialog.
430 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
431 // lists here? don't dare to change it now, but should be done later!
432 static wxList closing
;
434 if ( closing
.Member(this) )
437 closing
.Append(this);
439 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
440 cancelEvent
.SetEventObject( this );
441 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
443 closing
.DeleteObject(this);
446 void wxDialogBase
::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
448 SetBackgroundColour(wxSystemSettings
::GetColour(wxSYS_COLOUR_3DFACE
));