]>
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 // FIXME - temporary hack in absence of wxTopLevelWindow, should be always used
102 #ifdef wxTopLevelWindowNative
103 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
104 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
107 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
, wxTopLevelWindow
)
110 void wxDialogBase::Init()
113 m_affirmativeId
= wxID_OK
;
114 m_escapeId
= wxID_ANY
;
116 // the dialogs have this flag on by default to prevent the events from the
117 // dialog controls from reaching the parent frame which is usually
118 // undesirable and can lead to unexpected and hard to find bugs
119 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
121 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
122 m_container
.SetContainerWindow(this);
128 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
130 const wxChar
*lastSpace
= NULL
;
133 const wxChar
*lineStart
= text
.c_str();
134 for ( const wxChar
*p
= lineStart
; ; p
++ )
136 if ( IsStartOfNewLine() )
145 if ( *p
== _T('\n') || *p
== _T('\0') )
149 if ( *p
== _T('\0') )
159 if ( widthMax
>= 0 && lastSpace
)
162 win
->GetTextExtent(line
, &width
, NULL
);
164 if ( width
> widthMax
)
166 // remove the last word from this line
167 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
170 // go back to the last word of this line which we didn't
175 //else: no wrapping at all or impossible to wrap
180 class wxTextSizerWrapper
: public wxTextWrapper
183 wxTextSizerWrapper(wxWindow
*win
)
189 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
191 m_sizer
= new wxBoxSizer(wxVERTICAL
);
192 Wrap(m_win
, text
, widthMax
);
197 virtual void OnOutputLine(const wxString
& line
)
201 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
203 else // empty line, no need to create a control for it
206 m_hLine
= m_win
->GetCharHeight();
208 m_sizer
->Add(5, m_hLine
);
218 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
220 // I admit that this is complete bogus, but it makes
221 // message boxes work for pda screens temporarily..
223 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
226 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
229 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
230 // the static messages created by CreateTextSizer() (used by wxMessageBox,
231 // for example), we don't want this special meaning, so we need to quote it
232 wxString
text(message
);
233 text
.Replace(_T("&"), _T("&&"));
235 wxTextSizerWrapper
wrapper(this);
237 return wrapper
.CreateSizer(text
, widthMax
);
240 class wxLabelWrapper
: public wxTextWrapper
243 void WrapLabel(wxWindow
*text
, int widthMax
)
246 Wrap(text
, text
->GetLabel(), widthMax
);
247 text
->SetLabel(m_text
);
251 virtual void OnOutputLine(const wxString
& line
)
256 virtual void OnNewLine()
265 // NB: don't "factor out" the scope operator, SGI MIPSpro 7.3 (but not 7.4)
266 // gets confused if it doesn't immediately follow the class name
268 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
275 wxLabelWrapper wrapper
;
276 wrapper
.WrapLabel(this, width
);
279 #endif // wxUSE_STATTEXT
281 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
, bool separated
, wxCoord distance
)
283 #ifdef __SMARTPHONE__
284 wxUnusedVar(separated
);
285 wxUnusedVar(distance
);
287 wxDialog
* dialog
= (wxDialog
*) this;
289 dialog
->SetLeftMenu(wxID_OK
);
292 if (flags
& wxCANCEL
){
293 dialog
->SetRightMenu(wxID_CANCEL
);
297 dialog
->SetLeftMenu(wxID_YES
);
301 dialog
->SetLeftMenu(wxID_NO
);
303 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
306 #else // !__SMARTPHONE__
309 // PocketPC guidelines recommend for Ok/Cancel dialogs to use
310 // OK button located inside caption bar and implement Cancel functionality
311 // through Undo outside dialog. As native behaviour this will be default
312 // here but can be easily replaced with real wxButtons
313 // with "wince.dialog.real-ok-cancel" option set to 1
314 if ( ((flags
& ~(wxCANCEL
|wxNO_DEFAULT
))== wxOK
) &&
315 (wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel"))==0)
318 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
321 #endif // __POCKETPC__
325 wxSizer
* buttonSizer
= CreateStdDialogButtonSizer( flags
);
327 // Mac Human Interface Guidelines recommend not to use static lines as grouping elements
328 #if wxUSE_STATLINE && !defined(__WXMAC__)
332 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
333 topsizer
->Add( new wxStaticLine( this, wxID_ANY
), 0, wxEXPAND
| wxBOTTOM
, distance
);
334 topsizer
->Add( buttonSizer
, 0, wxEXPAND
);
337 #else // !wxUSE_STATLINE
339 wxUnusedVar(separated
);
340 wxUnusedVar(distance
);
343 #endif // wxUSE_STATLINE/!wxUSE_STATLINE
345 #else // !wxUSE_BUTTON
347 wxUnusedVar(separated
);
348 wxUnusedVar(distance
);
349 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
352 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
354 #endif // __SMARTPHONE__/!__SMARTPHONE__
359 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
361 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
364 wxButton
*yes
= NULL
;
368 ok
= new wxButton(this, wxID_OK
);
369 sizer
->AddButton(ok
);
372 if (flags
& wxCANCEL
){
373 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
374 sizer
->AddButton(cancel
);
378 yes
= new wxButton(this, wxID_YES
);
379 sizer
->AddButton(yes
);
383 no
= new wxButton(this, wxID_NO
);
384 sizer
->AddButton(no
);
388 wxButton
*help
= new wxButton(this, wxID_HELP
);
389 sizer
->AddButton(help
);
392 if (flags
& wxNO_DEFAULT
)
415 SetAffirmativeId(wxID_OK
);
416 else if (flags
& wxYES
)
417 SetAffirmativeId(wxID_YES
);
424 #endif // wxUSE_BUTTON