]>
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
;
112 // the dialogs have this flag on by default to prevent the events from the
113 // dialog controls from reaching the parent frame which is usually
114 // undesirable and can lead to unexpected and hard to find bugs
115 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
117 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
118 m_container
.SetContainerWindow(this);
124 void wxTextWrapper::Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
)
126 const wxChar
*lastSpace
= NULL
;
129 const wxChar
*lineStart
= text
.c_str();
130 for ( const wxChar
*p
= lineStart
; ; p
++ )
132 if ( IsStartOfNewLine() )
141 if ( *p
== _T('\n') || *p
== _T('\0') )
145 if ( *p
== _T('\0') )
155 if ( widthMax
>= 0 && lastSpace
)
158 win
->GetTextExtent(line
, &width
, NULL
);
160 if ( width
> widthMax
)
162 // remove the last word from this line
163 line
.erase(lastSpace
- lineStart
, p
+ 1 - lineStart
);
166 // go back to the last word of this line which we didn't
171 //else: no wrapping at all or impossible to wrap
176 class wxTextSizerWrapper
: public wxTextWrapper
179 wxTextSizerWrapper(wxWindow
*win
)
185 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
187 m_sizer
= new wxBoxSizer(wxVERTICAL
);
188 Wrap(m_win
, text
, widthMax
);
193 virtual void OnOutputLine(const wxString
& line
)
197 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
199 else // empty line, no need to create a control for it
202 m_hLine
= m_win
->GetCharHeight();
204 m_sizer
->Add(5, m_hLine
);
214 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
216 // I admit that this is complete bogus, but it makes
217 // message boxes work for pda screens temporarily..
219 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
222 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
225 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
226 // the static messages created by CreateTextSizer() (used by wxMessageBox,
227 // for example), we don't want this special meaning, so we need to quote it
228 wxString
text(message
);
229 text
.Replace(_T("&"), _T("&&"));
231 wxTextSizerWrapper
wrapper(this);
233 return wrapper
.CreateSizer(text
, widthMax
);
236 class wxLabelWrapper
: public wxTextWrapper
239 void WrapLabel(wxWindow
*text
, int widthMax
)
242 Wrap(text
, text
->GetLabel(), widthMax
);
243 text
->SetLabel(m_text
);
247 virtual void OnOutputLine(const wxString
& line
)
252 virtual void OnNewLine()
262 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
269 wxLabelWrapper wrapper
;
270 wrapper
.WrapLabel(this, width
);
273 #endif // wxUSE_STATTEXT
277 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
)
279 #ifdef __SMARTPHONE__
280 wxDialog
* dialog
= (wxDialog
*) this;
282 dialog
->SetLeftMenu(wxID_OK
);
285 if (flags
& wxCANCEL
){
286 dialog
->SetRightMenu(wxID_CANCEL
);
290 dialog
->SetLeftMenu(wxID_YES
);
294 dialog
->SetLeftMenu(wxID_NO
);
296 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
299 return CreateStdDialogButtonSizer( flags
);
303 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
305 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
307 wxButton
*yes
= NULL
;
311 ok
= new wxButton(this, wxID_OK
);
312 sizer
->AddButton(ok
);
315 if (flags
& wxCANCEL
){
316 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
317 sizer
->AddButton(cancel
);
321 yes
= new wxButton(this, wxID_YES
);
322 sizer
->AddButton(yes
);
326 no
= new wxButton(this, wxID_NO
);
327 sizer
->AddButton(no
);
331 wxButton
*help
= new wxButton(this, wxID_HELP
);
332 sizer
->AddButton(help
);
335 if (flags
& wxNO_DEFAULT
)
358 SetAffirmativeId(wxID_OK
);
359 else if (flags
& wxYES
)
360 SetAffirmativeId(wxID_YES
);
368 #endif // wxUSE_BUTTON