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"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 // this class is used to wrap the text on word boundary: wrapping is done by
49 // calling OnStartLine() and OnOutputLine() functions
53 wxTextWrapper() { m_eol
= false; }
55 // win is used for getting the font, text is the text to wrap, width is the
56 // max line width or -1 to disable wrapping
57 void Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
);
59 // we don't need it, but just to avoid compiler warnings
60 virtual ~wxTextWrapper() { }
64 virtual void OnOutputLine(const wxString
& line
) = 0;
66 // called at the start of every new line (except the very first one)
67 virtual void OnNewLine() { }
70 // call OnOutputLine() and set m_eol to true
71 void DoOutputLine(const wxString
& line
)
78 // this function is a destructive inspector: when it returns true it also
79 // resets the flag to false so calling it again woulnd't return true any
81 bool IsStartOfNewLine()
95 #endif // wxUSE_STATTEXT
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
102 EVT_BUTTON(wxID_ANY
, wxDialogBase::OnButton
)
104 EVT_CLOSE(wxDialogBase::OnCloseWindow
)
106 EVT_CHAR_HOOK(wxDialogBase::OnCharHook
)
108 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
111 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
, wxTopLevelWindow
)
113 void wxDialogBase::Init()
116 m_affirmativeId
= wxID_OK
;
117 m_escapeId
= wxID_ANY
;
119 // the dialogs have this flag on by default to prevent the events from the
120 // dialog controls from reaching the parent frame which is usually
121 // undesirable and can lead to unexpected and hard to find bugs
122 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
124 m_container
.SetContainerWindow(this);
129 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
131 const wxChar
*lastSpace
= NULL
;
134 const wxChar
*lineStart
= text
.c_str();
135 for ( const wxChar
*p
= lineStart
; ; p
++ )
137 if ( IsStartOfNewLine() )
146 if ( *p
== _T('\n') || *p
== _T('\0') )
150 if ( *p
== _T('\0') )
160 if ( widthMax
>= 0 && lastSpace
)
163 win
->GetTextExtent(line
, &width
, NULL
);
165 if ( width
> widthMax
)
167 // remove the last word from this line
168 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
171 // go back to the last word of this line which we didn't
176 //else: no wrapping at all or impossible to wrap
181 class wxTextSizerWrapper
: public wxTextWrapper
184 wxTextSizerWrapper(wxWindow
*win
)
190 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
192 m_sizer
= new wxBoxSizer(wxVERTICAL
);
193 Wrap(m_win
, text
, widthMax
);
198 virtual void OnOutputLine(const wxString
& line
)
202 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
204 else // empty line, no need to create a control for it
207 m_hLine
= m_win
->GetCharHeight();
209 m_sizer
->Add(5, m_hLine
);
219 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
221 // I admit that this is complete bogus, but it makes
222 // message boxes work for pda screens temporarily..
224 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
227 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
230 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
231 // the static messages created by CreateTextSizer() (used by wxMessageBox,
232 // for example), we don't want this special meaning, so we need to quote it
233 wxString
text(message
);
234 text
.Replace(_T("&"), _T("&&"));
236 wxTextSizerWrapper
wrapper(this);
238 return wrapper
.CreateSizer(text
, widthMax
);
241 class wxLabelWrapper
: public wxTextWrapper
244 void WrapLabel(wxWindow
*text
, int widthMax
)
247 Wrap(text
, text
->GetLabel(), widthMax
);
248 text
->SetLabel(m_text
);
252 virtual void OnOutputLine(const wxString
& line
)
257 virtual void OnNewLine()
266 // NB: don't "factor out" the scope operator, SGI MIPSpro 7.3 (but not 7.4)
267 // gets confused if it doesn't immediately follow the class name
269 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
276 wxLabelWrapper wrapper
;
277 wrapper
.WrapLabel(this, width
);
280 #endif // wxUSE_STATTEXT
282 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
284 wxSizer
*sizer
= NULL
;
286 #ifdef __SMARTPHONE__
287 wxDialog
* dialog
= (wxDialog
*) this;
289 dialog
->SetLeftMenu(wxID_OK
);
291 if ( flags
& wxCANCEL
)
292 dialog
->SetRightMenu(wxID_CANCEL
);
295 dialog
->SetLeftMenu(wxID_YES
);
298 dialog
->SetRightMenu(wxID_NO
);
299 #else // !__SMARTPHONE__
304 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
305 // located inside caption bar and implement Cancel functionality through
306 // Undo outside dialog. As native behaviour this will be default here but
307 // can be replaced with real wxButtons by setting the option below to 1
308 if ( (flags
& ~(wxCANCEL
|wxNO_DEFAULT
)) != wxOK
||
309 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
310 #endif // __POCKETPC__
312 sizer
= CreateStdDialogButtonSizer(flags
);
314 #endif // wxUSE_BUTTON
316 #endif // __SMARTPHONE__/!__SMARTPHONE__
321 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
323 wxSizer
*sizer
= CreateButtonSizer(flags
);
327 // Mac Human Interface Guidelines recommend not to use static lines as
329 #if wxUSE_STATLINE && !defined(__WXMAC__)
330 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
331 topsizer
->Add(new wxStaticLine(this),
332 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
333 topsizer
->Add(sizer
, wxSizerFlags().Expand());
335 #endif // wxUSE_STATLINE
342 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
344 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
347 wxButton
*yes
= NULL
;
352 ok
= new wxButton(this, wxID_OK
);
353 sizer
->AddButton(ok
);
356 if (flags
& wxCANCEL
)
358 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
359 sizer
->AddButton(cancel
);
364 yes
= new wxButton(this, wxID_YES
);
365 sizer
->AddButton(yes
);
370 no
= new wxButton(this, wxID_NO
);
371 sizer
->AddButton(no
);
376 wxButton
*help
= new wxButton(this, wxID_HELP
);
377 sizer
->AddButton(help
);
380 if (flags
& wxNO_DEFAULT
)
403 SetAffirmativeId(wxID_OK
);
404 else if (flags
& wxYES
)
405 SetAffirmativeId(wxID_YES
);
412 #endif // wxUSE_BUTTON
414 // ----------------------------------------------------------------------------
415 // standard buttons handling
416 // ----------------------------------------------------------------------------
418 void wxDialogBase::EndDialog(int rc
)
426 void wxDialogBase::AcceptAndClose()
428 if ( Validate() && TransferDataFromWindow() )
430 EndDialog(m_affirmativeId
);
434 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
436 m_affirmativeId
= affirmativeId
;
439 void wxDialogBase::SetEscapeId(int escapeId
)
441 m_escapeId
= escapeId
;
444 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
447 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
449 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
452 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
453 event
.SetEventObject(btn
);
454 btn
->GetEventHandler()->ProcessEvent(event
);
457 #else // !wxUSE_BUTTON
460 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
463 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
465 // for most platforms, Esc key is used to close the dialogs
466 return event
.GetKeyCode() == WXK_ESCAPE
&&
467 event
.GetModifiers() == wxMOD_NONE
;
470 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
472 if ( event
.GetKeyCode() == WXK_ESCAPE
)
474 int idCancel
= GetEscapeId();
478 // don't handle Esc specially at all
482 // this value is special: it means translate Esc to wxID_CANCEL
483 // but if there is no such button, then fall back to wxID_OK
484 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
486 idCancel
= GetAffirmativeId();
490 // translate Esc to button press for the button with given id
491 if ( EmulateButtonClickIfPresent(idCancel
) )
499 void wxDialogBase::OnButton(wxCommandEvent
& event
)
501 const int id
= event
.GetId();
502 if ( id
== GetAffirmativeId() )
506 else if ( id
== wxID_APPLY
)
509 TransferDataFromWindow();
511 // TODO: disable the Apply button until things change again
513 else if ( id
== GetEscapeId() ||
514 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
516 EndDialog(wxID_CANCEL
);
518 else // not a standard button
524 // ----------------------------------------------------------------------------
525 // other event handlers
526 // ----------------------------------------------------------------------------
528 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
530 // We'll send a Cancel message by default, which may close the dialog.
531 // Check for looping if the Cancel event handler calls Close().
533 // Note that if a cancel button and handler aren't present in the dialog,
534 // nothing will happen when you close the dialog via the window manager, or
535 // via Close(). We wouldn't want to destroy the dialog by default, since
536 // the dialog may have been created on the stack. However, this does mean
537 // that calling dialog->Close() won't delete the dialog unless the handler
538 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
539 // destroy the dialog. The default OnCancel (above) simply ends a modal
540 // dialog, and hides a modeless dialog.
542 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
543 // lists here? don't dare to change it now, but should be done later!
544 static wxList closing
;
546 if ( closing
.Member(this) )
549 closing
.Append(this);
551 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
552 cancelEvent
.SetEventObject( this );
553 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
555 closing
.DeleteObject(this);
558 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
560 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));