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();
74 wxWindow
*wxDialogBase::GetParentForModalDialog(wxWindow
*parent
) const
76 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
77 // in an unfocused dialog, so try to find a valid parent for it:
79 parent
= wxGetTopLevelParent(parent
);
81 if ( !parent
|| parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) )
82 parent
= wxTheApp
->GetTopWindow();
84 if ( parent
&& parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) )
86 // can't use this one, it's going to disappear
95 class wxTextSizerWrapper
: public wxTextWrapper
98 wxTextSizerWrapper(wxWindow
*win
)
104 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
106 m_sizer
= new wxBoxSizer(wxVERTICAL
);
107 Wrap(m_win
, text
, widthMax
);
112 virtual void OnOutputLine(const wxString
& line
)
116 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
118 else // empty line, no need to create a control for it
121 m_hLine
= m_win
->GetCharHeight();
123 m_sizer
->Add(5, m_hLine
);
133 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
135 // I admit that this is complete bogus, but it makes
136 // message boxes work for pda screens temporarily..
138 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
141 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
144 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
145 // the static messages created by CreateTextSizer() (used by wxMessageBox,
146 // for example), we don't want this special meaning, so we need to quote it
147 wxString
text(message
);
148 text
.Replace(_T("&"), _T("&&"));
150 wxTextSizerWrapper
wrapper(this);
152 return wrapper
.CreateSizer(text
, widthMax
);
155 #endif // wxUSE_STATTEXT
157 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
159 wxSizer
*sizer
= NULL
;
161 #ifdef __SMARTPHONE__
162 wxDialog
* dialog
= (wxDialog
*) this;
164 dialog
->SetLeftMenu(wxID_OK
);
166 if ( flags
& wxCANCEL
)
167 dialog
->SetRightMenu(wxID_CANCEL
);
170 dialog
->SetLeftMenu(wxID_YES
);
173 dialog
->SetRightMenu(wxID_NO
);
174 #else // !__SMARTPHONE__
179 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
180 // located inside caption bar and implement Cancel functionality through
181 // Undo outside dialog. As native behaviour this will be default here but
182 // can be replaced with real wxButtons by setting the option below to 1
183 if ( (flags
& ~(wxCANCEL
|wxNO_DEFAULT
)) != wxOK
||
184 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
185 #endif // __POCKETPC__
187 sizer
= CreateStdDialogButtonSizer(flags
);
189 #endif // wxUSE_BUTTON
191 #endif // __SMARTPHONE__/!__SMARTPHONE__
196 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
198 wxSizer
*sizer
= CreateButtonSizer(flags
);
202 // Mac Human Interface Guidelines recommend not to use static lines as
204 #if wxUSE_STATLINE && !defined(__WXMAC__)
205 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
206 topsizer
->Add(new wxStaticLine(this),
207 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
208 topsizer
->Add(sizer
, wxSizerFlags().Expand());
210 #endif // wxUSE_STATLINE
217 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
219 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
222 wxButton
*yes
= NULL
;
227 ok
= new wxButton(this, wxID_OK
);
228 sizer
->AddButton(ok
);
231 if (flags
& wxCANCEL
)
233 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
234 sizer
->AddButton(cancel
);
239 yes
= new wxButton(this, wxID_YES
);
240 sizer
->AddButton(yes
);
245 no
= new wxButton(this, wxID_NO
);
246 sizer
->AddButton(no
);
251 wxButton
*apply
= new wxButton(this, wxID_APPLY
);
252 sizer
->AddButton(apply
);
257 wxButton
*close
= new wxButton(this, wxID_CLOSE
);
258 sizer
->AddButton(close
);
263 wxButton
*help
= new wxButton(this, wxID_HELP
);
264 sizer
->AddButton(help
);
267 if (flags
& wxNO_DEFAULT
)
290 SetAffirmativeId(wxID_OK
);
291 else if (flags
& wxYES
)
292 SetAffirmativeId(wxID_YES
);
299 #endif // wxUSE_BUTTON
301 // ----------------------------------------------------------------------------
302 // standard buttons handling
303 // ----------------------------------------------------------------------------
305 void wxDialogBase::EndDialog(int rc
)
313 void wxDialogBase::AcceptAndClose()
315 if ( Validate() && TransferDataFromWindow() )
317 EndDialog(m_affirmativeId
);
321 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
323 m_affirmativeId
= affirmativeId
;
326 void wxDialogBase::SetEscapeId(int escapeId
)
328 m_escapeId
= escapeId
;
331 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
334 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
336 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
339 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
340 event
.SetEventObject(btn
);
341 btn
->GetEventHandler()->ProcessEvent(event
);
344 #else // !wxUSE_BUTTON
347 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
350 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
352 // for most platforms, Esc key is used to close the dialogs
353 return event
.GetKeyCode() == WXK_ESCAPE
&&
354 event
.GetModifiers() == wxMOD_NONE
;
357 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
359 if ( event
.GetKeyCode() == WXK_ESCAPE
)
361 int idCancel
= GetEscapeId();
365 // don't handle Esc specially at all
369 // this value is special: it means translate Esc to wxID_CANCEL
370 // but if there is no such button, then fall back to wxID_OK
371 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
373 idCancel
= GetAffirmativeId();
377 // translate Esc to button press for the button with given id
378 if ( EmulateButtonClickIfPresent(idCancel
) )
386 void wxDialogBase::OnButton(wxCommandEvent
& event
)
388 const int id
= event
.GetId();
389 if ( id
== GetAffirmativeId() )
393 else if ( id
== wxID_APPLY
)
396 TransferDataFromWindow();
398 // TODO: disable the Apply button until things change again
400 else if ( id
== GetEscapeId() ||
401 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
403 EndDialog(wxID_CANCEL
);
405 else // not a standard button
411 // ----------------------------------------------------------------------------
412 // other event handlers
413 // ----------------------------------------------------------------------------
415 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
417 // We'll send a Cancel message by default, which may close the dialog.
418 // Check for looping if the Cancel event handler calls Close().
420 // Note that if a cancel button and handler aren't present in the dialog,
421 // nothing will happen when you close the dialog via the window manager, or
422 // via Close(). We wouldn't want to destroy the dialog by default, since
423 // the dialog may have been created on the stack. However, this does mean
424 // that calling dialog->Close() won't delete the dialog unless the handler
425 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
426 // destroy the dialog. The default OnCancel (above) simply ends a modal
427 // dialog, and hides a modeless dialog.
429 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
430 // lists here? don't dare to change it now, but should be done later!
431 static wxList closing
;
433 if ( closing
.Member(this) )
436 closing
.Append(this);
438 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
439 cancelEvent
.SetEventObject( this );
440 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
442 closing
.DeleteObject(this);
445 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
447 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));