]>
git.saurik.com Git - wxWidgets.git/blob - src/common/dlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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/button.h"
36 #include "wx/containr.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 // this class is used to wrap the text on word boundary: wrapping is done by
46 // calling OnStartLine() and OnOutputLine() functions
50 wxTextWrapper() { m_eol
= false; }
52 // win is used for getting the font, text is the text to wrap, width is the
53 // max line width or -1 to disable wrapping
54 void Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
);
56 // we don't need it, but just to avoid compiler warnings
57 virtual ~wxTextWrapper() { }
61 virtual void OnOutputLine(const wxString
& line
) = 0;
63 // called at the start of every new line (except the very first one)
64 virtual void OnNewLine() { }
67 // call OnOutputLine() and set m_eol to true
68 void DoOutputLine(const wxString
& line
)
75 // this function is a destructive inspector: when it returns true it also
76 // resets the flag to false so calling it again woulnd't return true any
78 bool IsStartOfNewLine()
92 #endif // wxUSE_STATTEXT
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // FIXME - temporary hack in absence of wxTopLevelWindow, should be always used
99 #ifdef wxTopLevelWindowNative
100 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
101 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
104 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
)
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 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
119 m_container
.SetContainerWindow(this);
125 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
127 const wxChar
*lastSpace
= NULL
;
130 const wxChar
*lineStart
= text
.c_str();
131 for ( const wxChar
*p
= lineStart
; ; p
++ )
133 if ( IsStartOfNewLine() )
142 if ( *p
== _T('\n') || *p
== _T('\0') )
146 if ( *p
== _T('\0') )
156 if ( widthMax
>= 0 && lastSpace
)
159 win
->GetTextExtent(line
, &width
, NULL
);
161 if ( width
> widthMax
)
163 // remove the last word from this line
164 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
167 // go back to the last word of this line which we didn't
172 //else: no wrapping at all or impossible to wrap
177 class wxTextSizerWrapper
: public wxTextWrapper
180 wxTextSizerWrapper(wxWindow
*win
)
186 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
188 m_sizer
= new wxBoxSizer(wxVERTICAL
);
189 Wrap(m_win
, text
, widthMax
);
194 virtual void OnOutputLine(const wxString
& line
)
198 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
200 else // empty line, no need to create a control for it
203 m_hLine
= m_win
->GetCharHeight();
205 m_sizer
->Add(5, m_hLine
);
215 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
217 // I admit that this is complete bogus, but it makes
218 // message boxes work for pda screens temporarily..
220 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
223 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
226 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
227 // the static messages created by CreateTextSizer() (used by wxMessageBox,
228 // for example), we don't want this special meaning, so we need to quote it
229 wxString
text(message
);
230 text
.Replace(_T("&"), _T("&&"));
232 wxTextSizerWrapper
wrapper(this);
234 return wrapper
.CreateSizer(text
, widthMax
);
237 class wxLabelWrapper
: public wxTextWrapper
240 void WrapLabel(wxWindow
*text
, int widthMax
)
243 Wrap(text
, text
->GetLabel(), widthMax
);
244 text
->SetLabel(m_text
);
248 virtual void OnOutputLine(const wxString
& line
)
253 virtual void OnNewLine()
263 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
270 wxLabelWrapper wrapper
;
271 wrapper
.WrapLabel(this, width
);
274 #endif // wxUSE_STATTEXT
278 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
)
280 #ifdef __SMARTPHONE__
281 wxDialog
* dialog
= (wxDialog
*) this;
283 dialog
->SetLeftMenu(wxID_OK
);
286 if (flags
& wxCANCEL
){
287 dialog
->SetRightMenu(wxID_CANCEL
);
291 dialog
->SetLeftMenu(wxID_YES
);
295 dialog
->SetLeftMenu(wxID_NO
);
297 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
300 return CreateStdDialogButtonSizer( flags
);
304 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
306 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
308 wxButton
*yes
= NULL
;
312 ok
= new wxButton(this, wxID_OK
);
313 sizer
->AddButton(ok
);
316 if (flags
& wxCANCEL
){
317 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
318 sizer
->AddButton(cancel
);
322 yes
= new wxButton(this, wxID_YES
);
323 sizer
->AddButton(yes
);
327 no
= new wxButton(this, wxID_NO
);
328 sizer
->AddButton(no
);
332 wxButton
*help
= new wxButton(this, wxID_HELP
);
333 sizer
->AddButton(help
);
336 if (flags
& wxNO_DEFAULT
)
359 SetAffirmativeId(wxID_OK
);
360 else if (flags
& wxYES
)
361 SetAffirmativeId(wxID_YES
);
369 #endif // wxUSE_BUTTON