]>
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 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
182 // I admit that this is complete bogus, but it makes
183 // message boxes work for pda screens temporarily..
185 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
188 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
191 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
192 // the static messages created by CreateTextSizer() (used by wxMessageBox,
193 // for example), we don't want this special meaning, so we need to quote it
194 wxString
text(message
);
195 text
.Replace(_T("&"), _T("&&"));
198 class TextSizerWrapper
: public wxTextWrapper
201 TextSizerWrapper(wxWindow
*win
)
207 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
209 m_sizer
= new wxBoxSizer(wxVERTICAL
);
210 Wrap(m_win
, text
, widthMax
);
215 virtual void OnOutputLine(const wxString
& line
)
219 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
221 else // empty line, no need to create a control for it
224 m_hLine
= m_win
->GetCharHeight();
226 m_sizer
->Add(5, m_hLine
);
236 TextSizerWrapper
wrapper(this);
238 return wrapper
.CreateSizer(text
, widthMax
);
242 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
249 class LabelWrapper
: public wxTextWrapper
252 void WrapLabel(wxWindow
*text
, int widthMax
)
255 Wrap(text
, text
->GetLabel(), widthMax
);
256 text
->SetLabel(m_text
);
260 virtual void OnOutputLine(const wxString
& line
)
265 virtual void OnNewLine()
274 LabelWrapper wrapper
;
275 wrapper
.WrapLabel(this, width
);
278 #endif // wxUSE_STATTEXT
282 wxSizer
*wxDialogBase::CreateButtonSizer( long flags
)
284 #ifdef __SMARTPHONE__
285 wxDialog
* dialog
= (wxDialog
*) this;
287 dialog
->SetLeftMenu(wxID_OK
);
290 if (flags
& wxCANCEL
){
291 dialog
->SetRightMenu(wxID_CANCEL
);
295 dialog
->SetLeftMenu(wxID_YES
);
299 dialog
->SetLeftMenu(wxID_NO
);
301 wxBoxSizer
* sizer
= new wxBoxSizer(wxHORIZONTAL
);
304 return CreateStdDialogButtonSizer( flags
);
308 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
310 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
312 wxButton
*yes
= NULL
;
316 ok
= new wxButton(this, wxID_OK
);
317 sizer
->AddButton(ok
);
320 if (flags
& wxCANCEL
){
321 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
322 sizer
->AddButton(cancel
);
326 yes
= new wxButton(this, wxID_YES
);
327 sizer
->AddButton(yes
);
331 no
= new wxButton(this, wxID_NO
);
332 sizer
->AddButton(no
);
336 wxButton
*help
= new wxButton(this, wxID_HELP
);
337 sizer
->AddButton(help
);
342 if (flags
& wxNO_DEFAULT
)
365 SetAffirmativeId(wxID_OK
);
366 else if (flags
& wxYES
)
367 SetAffirmativeId(wxID_YES
);
373 #endif // wxUSE_BUTTON