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
, bool separated
, wxCoord distance
)
284 #ifdef __SMARTPHONE__
285 wxUnusedVar(separated
);
286 wxUnusedVar(distance
);
288 wxDialog
* dialog
= (wxDialog
*) this;
290 dialog
->SetLeftMenu(wxID_OK
);
293 if (flags
& wxCANCEL
){
294 dialog
->SetRightMenu(wxID_CANCEL
);
298 dialog
->SetLeftMenu(wxID_YES
);
302 dialog
->SetLeftMenu(wxID_NO
);
304 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
307 #else // !__SMARTPHONE__
310 // PocketPC guidelines recommend for Ok/Cancel dialogs to use
311 // OK button located inside caption bar and implement Cancel functionality
312 // through Undo outside dialog. As native behaviour this will be default
313 // here but can be easily replaced with real wxButtons
314 // with "wince.dialog.real-ok-cancel" option set to 1
315 if ( ((flags
& ~(wxCANCEL
|wxNO_DEFAULT
))== wxOK
) &&
316 (wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel"))==0)
319 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
322 #endif // __POCKETPC__
326 wxSizer
* buttonSizer
= CreateStdDialogButtonSizer( flags
);
328 // Mac Human Interface Guidelines recommend not to use static lines as grouping elements
329 #if wxUSE_STATLINE && !defined(__WXMAC__)
333 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
334 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxBOTTOM
, distance
);
335 topsizer
->Add( buttonSizer
, 0, wxEXPAND
);
338 #else // !wxUSE_STATLINE
340 wxUnusedVar(separated
);
341 wxUnusedVar(distance
);
344 #endif // wxUSE_STATLINE/!wxUSE_STATLINE
346 #else // !wxUSE_BUTTON
348 wxUnusedVar(separated
);
349 wxUnusedVar(distance
);
350 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
353 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
355 #endif // __SMARTPHONE__/!__SMARTPHONE__
360 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
362 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
365 wxButton
*yes
= NULL
;
369 ok
= new wxButton(this, wxID_OK
);
370 sizer
->AddButton(ok
);
373 if (flags
& wxCANCEL
){
374 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
375 sizer
->AddButton(cancel
);
379 yes
= new wxButton(this, wxID_YES
);
380 sizer
->AddButton(yes
);
384 no
= new wxButton(this, wxID_NO
);
385 sizer
->AddButton(no
);
389 wxButton
*help
= new wxButton(this, wxID_HELP
);
390 sizer
->AddButton(help
);
393 if (flags
& wxNO_DEFAULT
)
416 SetAffirmativeId(wxID_OK
);
417 else if (flags
& wxYES
)
418 SetAffirmativeId(wxID_YES
);
425 #endif // wxUSE_BUTTON
427 // ----------------------------------------------------------------------------
428 // standard buttons handling
429 // ----------------------------------------------------------------------------
431 void wxDialogBase::EndDialog(int rc
)
439 void wxDialogBase::AcceptAndClose()
441 if ( Validate() && TransferDataFromWindow() )
443 EndDialog(m_affirmativeId
);
447 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
449 m_affirmativeId
= affirmativeId
;
452 void wxDialogBase::SetEscapeId(int escapeId
)
454 m_escapeId
= escapeId
;
457 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
460 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
462 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
465 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
466 event
.SetEventObject(btn
);
467 btn
->GetEventHandler()->ProcessEvent(event
);
470 #else // !wxUSE_BUTTON
473 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
476 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
478 // for most platforms, Esc key is used to close the dialogs
479 return event
.GetKeyCode() == WXK_ESCAPE
&&
480 event
.GetModifiers() == wxMOD_NONE
;
483 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
485 if ( event
.GetKeyCode() == WXK_ESCAPE
)
487 int idCancel
= GetEscapeId();
491 // don't handle Esc specially at all
495 // this value is special: it means translate Esc to wxID_CANCEL
496 // but if there is no such button, then fall back to wxID_OK
497 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
499 idCancel
= GetAffirmativeId();
503 // translate Esc to button press for the button with given id
504 if ( EmulateButtonClickIfPresent(idCancel
) )
512 void wxDialogBase::OnButton(wxCommandEvent
& event
)
514 const int id
= event
.GetId();
515 if ( id
== GetAffirmativeId() )
519 else if ( id
== wxID_APPLY
)
522 TransferDataFromWindow();
524 // TODO: disable the Apply button until things change again
526 else if ( id
== GetEscapeId() ||
527 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
529 EndDialog(wxID_CANCEL
);
531 else // not a standard button
537 // ----------------------------------------------------------------------------
538 // other event handlers
539 // ----------------------------------------------------------------------------
541 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
543 // We'll send a Cancel message by default, which may close the dialog.
544 // Check for looping if the Cancel event handler calls Close().
546 // Note that if a cancel button and handler aren't present in the dialog,
547 // nothing will happen when you close the dialog via the window manager, or
548 // via Close(). We wouldn't want to destroy the dialog by default, since
549 // the dialog may have been created on the stack. However, this does mean
550 // that calling dialog->Close() won't delete the dialog unless the handler
551 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
552 // destroy the dialog. The default OnCancel (above) simply ends a modal
553 // dialog, and hides a modeless dialog.
555 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
556 // lists here? don't dare to change it now, but should be done later!
557 static wxList closing
;
559 if ( closing
.Member(this) )
562 closing
.Append(this);
564 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
565 cancelEvent
.SetEventObject( this );
566 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
568 closing
.DeleteObject(this);
571 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
573 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));