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_OK
, wxDialogBase::OnOK
)
103 EVT_BUTTON(wxID_APPLY
, wxDialogBase::OnApply
)
104 EVT_BUTTON(wxID_CANCEL
, wxDialogBase::OnCancel
)
106 EVT_CLOSE(wxDialogBase::OnCloseWindow
)
108 EVT_CHAR_HOOK(wxDialogBase::OnCharHook
)
110 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
113 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
, wxTopLevelWindow
)
115 void wxDialogBase::Init()
118 m_affirmativeId
= wxID_OK
;
119 m_escapeId
= wxID_ANY
;
121 // the dialogs have this flag on by default to prevent the events from the
122 // dialog controls from reaching the parent frame which is usually
123 // undesirable and can lead to unexpected and hard to find bugs
124 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
126 m_container
.SetContainerWindow(this);
131 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
133 const wxChar
*lastSpace
= NULL
;
136 const wxChar
*lineStart
= text
.c_str();
137 for ( const wxChar
*p
= lineStart
; ; p
++ )
139 if ( IsStartOfNewLine() )
148 if ( *p
== _T('\n') || *p
== _T('\0') )
152 if ( *p
== _T('\0') )
162 if ( widthMax
>= 0 && lastSpace
)
165 win
->GetTextExtent(line
, &width
, NULL
);
167 if ( width
> widthMax
)
169 // remove the last word from this line
170 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
173 // go back to the last word of this line which we didn't
178 //else: no wrapping at all or impossible to wrap
183 class wxTextSizerWrapper
: public wxTextWrapper
186 wxTextSizerWrapper(wxWindow
*win
)
192 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
194 m_sizer
= new wxBoxSizer(wxVERTICAL
);
195 Wrap(m_win
, text
, widthMax
);
200 virtual void OnOutputLine(const wxString
& line
)
204 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
206 else // empty line, no need to create a control for it
209 m_hLine
= m_win
->GetCharHeight();
211 m_sizer
->Add(5, m_hLine
);
221 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
223 // I admit that this is complete bogus, but it makes
224 // message boxes work for pda screens temporarily..
226 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
229 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
232 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
233 // the static messages created by CreateTextSizer() (used by wxMessageBox,
234 // for example), we don't want this special meaning, so we need to quote it
235 wxString
text(message
);
236 text
.Replace(_T("&"), _T("&&"));
238 wxTextSizerWrapper
wrapper(this);
240 return wrapper
.CreateSizer(text
, widthMax
);
243 class wxLabelWrapper
: public wxTextWrapper
246 void WrapLabel(wxWindow
*text
, int widthMax
)
249 Wrap(text
, text
->GetLabel(), widthMax
);
250 text
->SetLabel(m_text
);
254 virtual void OnOutputLine(const wxString
& line
)
259 virtual void OnNewLine()
268 // NB: don't "factor out" the scope operator, SGI MIPSpro 7.3 (but not 7.4)
269 // gets confused if it doesn't immediately follow the class name
271 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
278 wxLabelWrapper wrapper
;
279 wrapper
.WrapLabel(this, width
);
282 #endif // wxUSE_STATTEXT
284 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
, bool separated
, wxCoord distance
)
286 #ifdef __SMARTPHONE__
287 wxUnusedVar(separated
);
288 wxUnusedVar(distance
);
290 wxDialog
* dialog
= (wxDialog
*) this;
292 dialog
->SetLeftMenu(wxID_OK
);
295 if (flags
& wxCANCEL
){
296 dialog
->SetRightMenu(wxID_CANCEL
);
300 dialog
->SetLeftMenu(wxID_YES
);
304 dialog
->SetLeftMenu(wxID_NO
);
306 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
309 #else // !__SMARTPHONE__
312 // PocketPC guidelines recommend for Ok/Cancel dialogs to use
313 // OK button located inside caption bar and implement Cancel functionality
314 // through Undo outside dialog. As native behaviour this will be default
315 // here but can be easily replaced with real wxButtons
316 // with "wince.dialog.real-ok-cancel" option set to 1
317 if ( ((flags
& ~(wxCANCEL
|wxNO_DEFAULT
))== wxOK
) &&
318 (wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel"))==0)
321 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
324 #endif // __POCKETPC__
328 wxSizer
* buttonSizer
= CreateStdDialogButtonSizer( flags
);
330 // Mac Human Interface Guidelines recommend not to use static lines as grouping elements
331 #if wxUSE_STATLINE && !defined(__WXMAC__)
335 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
336 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxBOTTOM
, distance
);
337 topsizer
->Add( buttonSizer
, 0, wxEXPAND
);
340 #else // !wxUSE_STATLINE
342 wxUnusedVar(separated
);
343 wxUnusedVar(distance
);
346 #endif // wxUSE_STATLINE/!wxUSE_STATLINE
348 #else // !wxUSE_BUTTON
350 wxUnusedVar(separated
);
351 wxUnusedVar(distance
);
352 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
355 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
357 #endif // __SMARTPHONE__/!__SMARTPHONE__
362 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
364 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
367 wxButton
*yes
= NULL
;
371 ok
= new wxButton(this, wxID_OK
);
372 sizer
->AddButton(ok
);
375 if (flags
& wxCANCEL
){
376 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
377 sizer
->AddButton(cancel
);
381 yes
= new wxButton(this, wxID_YES
);
382 sizer
->AddButton(yes
);
386 no
= new wxButton(this, wxID_NO
);
387 sizer
->AddButton(no
);
391 wxButton
*help
= new wxButton(this, wxID_HELP
);
392 sizer
->AddButton(help
);
395 if (flags
& wxNO_DEFAULT
)
418 SetAffirmativeId(wxID_OK
);
419 else if (flags
& wxYES
)
420 SetAffirmativeId(wxID_YES
);
427 #endif // wxUSE_BUTTON
429 // ----------------------------------------------------------------------------
430 // event handling stuff
431 // ----------------------------------------------------------------------------
433 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
435 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
437 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
440 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
441 event
.SetEventObject(btn
);
442 btn
->GetEventHandler()->ProcessEvent(event
);
447 void wxDialogBase::EndDialog(int rc
)
455 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
457 // for most platforms, Esc key is used to close the dialogs
458 return event
.GetKeyCode() == WXK_ESCAPE
&&
459 event
.GetModifiers() == wxMOD_NONE
;
462 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
464 if ( event
.GetKeyCode() == WXK_ESCAPE
)
466 int idCancel
= GetEscapeId();
470 // don't handle Esc specially at all
474 // this value is special: it means translate Esc to wxID_CANCEL
475 // but if there is no such button, then fall back to wxID_OK
476 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
482 // translate Esc to button press for the button with given id
483 if ( EmulateButtonClickIfPresent(idCancel
) )
491 void wxDialogBase::OnOK(wxCommandEvent
& WXUNUSED(event
))
493 if ( Validate() && TransferDataFromWindow() )
499 void wxDialogBase::OnApply(wxCommandEvent
& WXUNUSED(event
))
502 TransferDataFromWindow();
504 // TODO probably need to disable the Apply button until things change again
507 void wxDialogBase::OnCancel(wxCommandEvent
& WXUNUSED(event
))
509 EndDialog(wxID_CANCEL
);
512 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
514 // We'll send a Cancel message by default, which may close the dialog.
515 // Check for looping if the Cancel event handler calls Close().
517 // Note that if a cancel button and handler aren't present in the dialog,
518 // nothing will happen when you close the dialog via the window manager, or
519 // via Close(). We wouldn't want to destroy the dialog by default, since
520 // the dialog may have been created on the stack. However, this does mean
521 // that calling dialog->Close() won't delete the dialog unless the handler
522 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
523 // destroy the dialog. The default OnCancel (above) simply ends a modal
524 // dialog, and hides a modeless dialog.
526 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
527 // lists here? don't dare to change it now, but should be done later!
528 static wxList closing
;
530 if ( closing
.Member(this) )
533 closing
.Append(this);
535 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
536 cancelEvent
.SetEventObject( this );
537 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
539 closing
.DeleteObject(this);
542 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& WXUNUSED(event
))
544 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));