]>
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"
28 #include "wx/button.h"
29 #include "wx/dialog.h"
30 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #include "wx/stattext.h"
35 #include "wx/containr.h"
38 #include "wx/statline.h"
39 #include "wx/sysopt.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // this class is used to wrap the text on word boundary: wrapping is done by
48 // calling OnStartLine() and OnOutputLine() functions
52 wxTextWrapper() { m_eol
= false; }
54 // win is used for getting the font, text is the text to wrap, width is the
55 // max line width or -1 to disable wrapping
56 void Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
);
58 // we don't need it, but just to avoid compiler warnings
59 virtual ~wxTextWrapper() { }
63 virtual void OnOutputLine(const wxString
& line
) = 0;
65 // called at the start of every new line (except the very first one)
66 virtual void OnNewLine() { }
69 // call OnOutputLine() and set m_eol to true
70 void DoOutputLine(const wxString
& line
)
77 // this function is a destructive inspector: when it returns true it also
78 // resets the flag to false so calling it again woulnd't return true any
80 bool IsStartOfNewLine()
94 #endif // wxUSE_STATTEXT
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // FIXME - temporary hack in absence of wxTopLevelWindow, should be always used
101 #ifdef wxTopLevelWindowNative
102 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
103 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
106 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
)
109 void wxDialogBase::Init()
112 m_affirmativeId
= wxID_OK
;
113 m_escapeId
= wxID_ANY
;
115 // the dialogs have this flag on by default to prevent the events from the
116 // dialog controls from reaching the parent frame which is usually
117 // undesirable and can lead to unexpected and hard to find bugs
118 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
120 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
121 m_container
.SetContainerWindow(this);
127 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
129 const wxChar
*lastSpace
= NULL
;
132 const wxChar
*lineStart
= text
.c_str();
133 for ( const wxChar
*p
= lineStart
; ; p
++ )
135 if ( IsStartOfNewLine() )
144 if ( *p
== _T('\n') || *p
== _T('\0') )
148 if ( *p
== _T('\0') )
158 if ( widthMax
>= 0 && lastSpace
)
161 win
->GetTextExtent(line
, &width
, NULL
);
163 if ( width
> widthMax
)
165 // remove the last word from this line
166 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
169 // go back to the last word of this line which we didn't
174 //else: no wrapping at all or impossible to wrap
179 class wxTextSizerWrapper
: public wxTextWrapper
182 wxTextSizerWrapper(wxWindow
*win
)
188 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
190 m_sizer
= new wxBoxSizer(wxVERTICAL
);
191 Wrap(m_win
, text
, widthMax
);
196 virtual void OnOutputLine(const wxString
& line
)
200 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
202 else // empty line, no need to create a control for it
205 m_hLine
= m_win
->GetCharHeight();
207 m_sizer
->Add(5, m_hLine
);
217 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
219 // I admit that this is complete bogus, but it makes
220 // message boxes work for pda screens temporarily..
222 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
225 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
228 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
229 // the static messages created by CreateTextSizer() (used by wxMessageBox,
230 // for example), we don't want this special meaning, so we need to quote it
231 wxString
text(message
);
232 text
.Replace(_T("&"), _T("&&"));
234 wxTextSizerWrapper
wrapper(this);
236 return wrapper
.CreateSizer(text
, widthMax
);
239 class wxLabelWrapper
: public wxTextWrapper
242 void WrapLabel(wxWindow
*text
, int widthMax
)
245 Wrap(text
, text
->GetLabel(), widthMax
);
246 text
->SetLabel(m_text
);
250 virtual void OnOutputLine(const wxString
& line
)
255 virtual void OnNewLine()
264 // NB: don't "factor out" the scope operator, SGI MIPSpro 7.3 (but not 7.4)
265 // gets confused if it doesn't immediately follow the class name
267 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
274 wxLabelWrapper wrapper
;
275 wrapper
.WrapLabel(this, width
);
278 #endif // wxUSE_STATTEXT
280 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
, bool separated
, wxCoord distance
)
282 #ifdef __SMARTPHONE__
283 wxUnusedVar(separated
);
284 wxUnusedVar(distance
);
286 wxDialog
* dialog
= (wxDialog
*) this;
288 dialog
->SetLeftMenu(wxID_OK
);
291 if (flags
& wxCANCEL
){
292 dialog
->SetRightMenu(wxID_CANCEL
);
296 dialog
->SetLeftMenu(wxID_YES
);
300 dialog
->SetLeftMenu(wxID_NO
);
302 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
305 #else // !__SMARTPHONE__
308 // PocketPC guidelines recommend for Ok/Cancel dialogs to use
309 // OK button located inside caption bar and implement Cancel functionality
310 // through Undo outside dialog. As native behaviour this will be default
311 // here but can be easily replaced with real wxButtons
312 // with "wince.dialog.real-ok-cancel" option set to 1
313 if ( ((flags
& ~(wxCANCEL
|wxNO_DEFAULT
))== wxOK
) &&
314 (wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel"))==0)
317 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
320 #endif // __POCKETPC__
324 wxSizer
* buttonSizer
= CreateStdDialogButtonSizer( flags
);
326 // Mac Human Interface Guidelines recommend not to use static lines as grouping elements
327 #if wxUSE_STATLINE && !defined(__WXMAC__)
331 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
332 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxBOTTOM
, distance
);
333 topsizer
->Add( buttonSizer
, 0, wxEXPAND
);
336 #else // !wxUSE_STATLINE
338 wxUnusedVar(separated
);
339 wxUnusedVar(distance
);
342 #endif // wxUSE_STATLINE/!wxUSE_STATLINE
344 #else // !wxUSE_BUTTON
346 wxUnusedVar(separated
);
347 wxUnusedVar(distance
);
348 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
351 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
353 #endif // __SMARTPHONE__/!__SMARTPHONE__
358 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
360 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
363 wxButton
*yes
= NULL
;
367 ok
= new wxButton(this, wxID_OK
);
368 sizer
->AddButton(ok
);
371 if (flags
& wxCANCEL
){
372 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
373 sizer
->AddButton(cancel
);
377 yes
= new wxButton(this, wxID_YES
);
378 sizer
->AddButton(yes
);
382 no
= new wxButton(this, wxID_NO
);
383 sizer
->AddButton(no
);
387 wxButton
*help
= new wxButton(this, wxID_HELP
);
388 sizer
->AddButton(help
);
391 if (flags
& wxNO_DEFAULT
)
414 SetAffirmativeId(wxID_OK
);
415 else if (flags
& wxYES
)
416 SetAffirmativeId(wxID_YES
);
423 #endif // wxUSE_BUTTON