]>
git.saurik.com Git - wxWidgets.git/blob - src/common/dlgcmn.cpp
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 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
105 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
, wxTopLevelWindow
)
107 void wxDialogBase::Init()
110 m_affirmativeId
= wxID_OK
;
111 m_escapeId
= wxID_ANY
;
113 // the dialogs have this flag on by default to prevent the events from the
114 // dialog controls from reaching the parent frame which is usually
115 // undesirable and can lead to unexpected and hard to find bugs
116 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
118 m_container
.SetContainerWindow(this);
123 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
125 const wxChar
*lastSpace
= NULL
;
128 const wxChar
*lineStart
= text
.c_str();
129 for ( const wxChar
*p
= lineStart
; ; p
++ )
131 if ( IsStartOfNewLine() )
140 if ( *p
== _T('\n') || *p
== _T('\0') )
144 if ( *p
== _T('\0') )
154 if ( widthMax
>= 0 && lastSpace
)
157 win
->GetTextExtent(line
, &width
, NULL
);
159 if ( width
> widthMax
)
161 // remove the last word from this line
162 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
165 // go back to the last word of this line which we didn't
170 //else: no wrapping at all or impossible to wrap
175 class wxTextSizerWrapper
: public wxTextWrapper
178 wxTextSizerWrapper(wxWindow
*win
)
184 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
186 m_sizer
= new wxBoxSizer(wxVERTICAL
);
187 Wrap(m_win
, text
, widthMax
);
192 virtual void OnOutputLine(const wxString
& line
)
196 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
198 else // empty line, no need to create a control for it
201 m_hLine
= m_win
->GetCharHeight();
203 m_sizer
->Add(5, m_hLine
);
213 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
215 // I admit that this is complete bogus, but it makes
216 // message boxes work for pda screens temporarily..
218 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
221 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
224 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
225 // the static messages created by CreateTextSizer() (used by wxMessageBox,
226 // for example), we don't want this special meaning, so we need to quote it
227 wxString
text(message
);
228 text
.Replace(_T("&"), _T("&&"));
230 wxTextSizerWrapper
wrapper(this);
232 return wrapper
.CreateSizer(text
, widthMax
);
235 class wxLabelWrapper
: public wxTextWrapper
238 void WrapLabel(wxWindow
*text
, int widthMax
)
241 Wrap(text
, text
->GetLabel(), widthMax
);
242 text
->SetLabel(m_text
);
246 virtual void OnOutputLine(const wxString
& line
)
251 virtual void OnNewLine()
260 // NB: don't "factor out" the scope operator, SGI MIPSpro 7.3 (but not 7.4)
261 // gets confused if it doesn't immediately follow the class name
263 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
270 wxLabelWrapper wrapper
;
271 wrapper
.WrapLabel(this, width
);
274 #endif // wxUSE_STATTEXT
276 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
, bool separated
, wxCoord distance
)
278 #ifdef __SMARTPHONE__
279 wxUnusedVar(separated
);
280 wxUnusedVar(distance
);
282 wxDialog
* dialog
= (wxDialog
*) this;
284 dialog
->SetLeftMenu(wxID_OK
);
287 if (flags
& wxCANCEL
){
288 dialog
->SetRightMenu(wxID_CANCEL
);
292 dialog
->SetLeftMenu(wxID_YES
);
296 dialog
->SetLeftMenu(wxID_NO
);
298 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
301 #else // !__SMARTPHONE__
304 // PocketPC guidelines recommend for Ok/Cancel dialogs to use
305 // OK button located inside caption bar and implement Cancel functionality
306 // through Undo outside dialog. As native behaviour this will be default
307 // here but can be easily replaced with real wxButtons
308 // with "wince.dialog.real-ok-cancel" option set to 1
309 if ( ((flags
& ~(wxCANCEL
|wxNO_DEFAULT
))== wxOK
) &&
310 (wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel"))==0)
313 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
316 #endif // __POCKETPC__
320 wxSizer
* buttonSizer
= CreateStdDialogButtonSizer( flags
);
322 // Mac Human Interface Guidelines recommend not to use static lines as grouping elements
323 #if wxUSE_STATLINE && !defined(__WXMAC__)
327 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
328 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxBOTTOM
, distance
);
329 topsizer
->Add( buttonSizer
, 0, wxEXPAND
);
332 #else // !wxUSE_STATLINE
334 wxUnusedVar(separated
);
335 wxUnusedVar(distance
);
338 #endif // wxUSE_STATLINE/!wxUSE_STATLINE
340 #else // !wxUSE_BUTTON
342 wxUnusedVar(separated
);
343 wxUnusedVar(distance
);
344 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
347 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
349 #endif // __SMARTPHONE__/!__SMARTPHONE__
354 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
356 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
359 wxButton
*yes
= NULL
;
363 ok
= new wxButton(this, wxID_OK
);
364 sizer
->AddButton(ok
);
367 if (flags
& wxCANCEL
){
368 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
369 sizer
->AddButton(cancel
);
373 yes
= new wxButton(this, wxID_YES
);
374 sizer
->AddButton(yes
);
378 no
= new wxButton(this, wxID_NO
);
379 sizer
->AddButton(no
);
383 wxButton
*help
= new wxButton(this, wxID_HELP
);
384 sizer
->AddButton(help
);
387 if (flags
& wxNO_DEFAULT
)
410 SetAffirmativeId(wxID_OK
);
411 else if (flags
& wxYES
)
412 SetAffirmativeId(wxID_YES
);
419 #endif // wxUSE_BUTTON