]>
git.saurik.com Git - wxWidgets.git/blob - src/common/dlgcmn.cpp
6d3e218315369cabf377c86f5475d7e20d7f306c
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "dialogbase.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/button.h"
33 #include "wx/dialog.h"
34 #include "wx/dcclient.h"
36 #include "wx/settings.h"
37 #include "wx/stattext.h"
39 #include "wx/button.h"
40 #include "wx/containr.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // this class is used to wrap the text on word boundary: wrapping is done by
50 // calling OnStartLine() and OnOutputLine() functions
54 wxTextWrapper() { m_eol
= false; }
56 // win is used for getting the font, text is the text to wrap, width is the
57 // max line width or -1 to disable wrapping
58 void Wrap(wxWindow
*win
, const wxString
& text
, int widthMax
);
62 virtual void OnOutputLine(const wxString
& line
) = 0;
64 // called at the start of every new line (except the very first one)
65 virtual void OnNewLine() { }
68 // call OnOutputLine() and set m_eol to true
69 void DoOutputLine(const wxString
& line
)
76 // this function is a destructive inspector: when it returns true it also
77 // resets the flag to false so calling it again woulnd't return true any
79 bool IsStartOfNewLine()
93 #endif // wxUSE_STATTEXT
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // FIXME - temporary hack in absence of wxTopLevelWindow, should be always used
100 #ifdef wxTopLevelWindowNative
101 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
102 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
105 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
)
108 void wxDialogBase::Init()
111 m_affirmativeId
= wxID_OK
;
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 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
179 // I admit that this is complete bogus, but it makes
180 // message boxes work for pda screens temporarily..
182 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
185 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
188 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
189 // the static messages created by CreateTextSizer() (used by wxMessageBox,
190 // for example), we don't want this special meaning, so we need to quote it
191 wxString
text(message
);
192 text
.Replace(_T("&"), _T("&&"));
195 class TextSizerWrapper
: public wxTextWrapper
198 TextSizerWrapper(wxWindow
*win
)
204 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
206 m_sizer
= new wxBoxSizer(wxVERTICAL
);
207 Wrap(m_win
, text
, widthMax
);
212 virtual void OnOutputLine(const wxString
& line
)
216 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
218 else // empty line, no need to create a control for it
221 m_hLine
= m_win
->GetCharHeight();
223 m_sizer
->Add(5, m_hLine
);
233 TextSizerWrapper
wrapper(this);
235 return wrapper
.CreateSizer(text
, widthMax
);
238 void wxStaticTextBase::Wrap(int width
)
240 class LabelWrapper
: public wxTextWrapper
243 void WrapLabel(wxStaticTextBase
*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 LabelWrapper wrapper
;
266 wrapper
.WrapLabel(this, width
);
269 #endif // wxUSE_STATTEXT
273 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
)
275 #ifdef __SMARTPHONE__
276 wxDialog
* dialog
= (wxDialog
*) this;
278 dialog
->SetLeftMenu(wxID_OK
);
281 if (flags
& wxCANCEL
){
282 dialog
->SetRightMenu(wxID_CANCEL
);
286 dialog
->SetLeftMenu(wxID_YES
);
290 dialog
->SetLeftMenu(wxID_NO
);
292 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
295 return CreateStdDialogButtonSizer( flags
);
299 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
301 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
303 wxButton
*yes
= NULL
;
307 ok
= new wxButton(this, wxID_OK
);
308 sizer
->AddButton(ok
);
311 if (flags
& wxCANCEL
){
312 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
313 sizer
->AddButton(cancel
);
317 yes
= new wxButton(this, wxID_YES
);
318 sizer
->AddButton(yes
);
322 no
= new wxButton(this, wxID_NO
);
323 sizer
->AddButton(no
);
327 wxButton
*help
= new wxButton(this, wxID_HELP
);
328 sizer
->AddButton(help
);
333 if (flags
& wxNO_DEFAULT
)
356 SetAffirmativeId(wxID_OK
);
357 else if (flags
& wxYES
)
358 SetAffirmativeId(wxID_YES
);
364 #endif // wxUSE_BUTTON