]>
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 #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
);
60 // we don't need it, but just to avoid compiler warnings
61 virtual ~wxTextWrapper() { }
65 virtual void OnOutputLine(const wxString
& line
) = 0;
67 // called at the start of every new line (except the very first one)
68 virtual void OnNewLine() { }
71 // call OnOutputLine() and set m_eol to true
72 void DoOutputLine(const wxString
& line
)
79 // this function is a destructive inspector: when it returns true it also
80 // resets the flag to false so calling it again woulnd't return true any
82 bool IsStartOfNewLine()
96 #endif // wxUSE_STATTEXT
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // FIXME - temporary hack in absence of wxTopLevelWindow, should be always used
103 #ifdef wxTopLevelWindowNative
104 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
105 WX_EVENT_TABLE_CONTROL_CONTAINER(wxDialogBase
)
108 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase
)
111 void wxDialogBase::Init()
114 m_affirmativeId
= wxID_OK
;
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()
266 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
273 wxLabelWrapper wrapper
;
274 wrapper
.WrapLabel(this, width
);
277 #endif // wxUSE_STATTEXT
281 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
)
283 #ifdef __SMARTPHONE__
284 wxDialog
* dialog
= (wxDialog
*) this;
286 dialog
->SetLeftMenu(wxID_OK
);
289 if (flags
& wxCANCEL
){
290 dialog
->SetRightMenu(wxID_CANCEL
);
294 dialog
->SetLeftMenu(wxID_YES
);
298 dialog
->SetLeftMenu(wxID_NO
);
300 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
303 return CreateStdDialogButtonSizer( flags
);
307 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
309 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
311 wxButton
*yes
= NULL
;
315 ok
= new wxButton(this, wxID_OK
);
316 sizer
->AddButton(ok
);
319 if (flags
& wxCANCEL
){
320 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
321 sizer
->AddButton(cancel
);
325 yes
= new wxButton(this, wxID_YES
);
326 sizer
->AddButton(yes
);
330 no
= new wxButton(this, wxID_NO
);
331 sizer
->AddButton(no
);
335 wxButton
*help
= new wxButton(this, wxID_HELP
);
336 sizer
->AddButton(help
);
339 if (flags
& wxNO_DEFAULT
)
362 SetAffirmativeId(wxID_OK
);
363 else if (flags
& wxYES
)
364 SetAffirmativeId(wxID_YES
);
372 #endif // wxUSE_BUTTON