]> git.saurik.com Git - wxWidgets.git/blob - src/common/dlgcmn.cpp
wxMultiChoiceDialog uses now wxCheckListBox if possible, wxListBox if not
[wxWidgets.git] / src / common / dlgcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/dlgcmn.cpp
3 // Purpose: common (to all ports) wxDialog functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 28.06.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/button.h"
29 #include "wx/dialog.h"
30 #include "wx/dcclient.h"
31 #include "wx/intl.h"
32 #include "wx/settings.h"
33 #include "wx/stattext.h"
34 #include "wx/sizer.h"
35 #include "wx/button.h"
36 #include "wx/containr.h"
37 #endif
38
39 #if wxUSE_STATTEXT
40
41 // ----------------------------------------------------------------------------
42 // wxTextWrapper
43 // ----------------------------------------------------------------------------
44
45 // this class is used to wrap the text on word boundary: wrapping is done by
46 // calling OnStartLine() and OnOutputLine() functions
47 class wxTextWrapper
48 {
49 public:
50 wxTextWrapper() { m_eol = false; }
51
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);
55
56 // we don't need it, but just to avoid compiler warnings
57 virtual ~wxTextWrapper() { }
58
59 protected:
60 // line may be empty
61 virtual void OnOutputLine(const wxString& line) = 0;
62
63 // called at the start of every new line (except the very first one)
64 virtual void OnNewLine() { }
65
66 private:
67 // call OnOutputLine() and set m_eol to true
68 void DoOutputLine(const wxString& line)
69 {
70 OnOutputLine(line);
71
72 m_eol = true;
73 }
74
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
77 // more
78 bool IsStartOfNewLine()
79 {
80 if ( !m_eol )
81 return false;
82
83 m_eol = false;
84
85 return true;
86 }
87
88
89 bool m_eol;
90 };
91
92 #endif // wxUSE_STATTEXT
93
94 // ----------------------------------------------------------------------------
95 // wxDialogBase
96 // ----------------------------------------------------------------------------
97
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)
102 END_EVENT_TABLE()
103
104 WX_DELEGATE_TO_CONTROL_CONTAINER(wxDialogBase)
105 #endif
106
107 void wxDialogBase::Init()
108 {
109 m_returnCode = 0;
110 m_affirmativeId = wxID_OK;
111 m_escapeId = wxID_ANY;
112
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);
117
118 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
119 m_container.SetContainerWindow(this);
120 #endif
121 }
122
123 #if wxUSE_STATTEXT
124
125 void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
126 {
127 const wxChar *lastSpace = NULL;
128 wxString line;
129
130 const wxChar *lineStart = text.c_str();
131 for ( const wxChar *p = lineStart; ; p++ )
132 {
133 if ( IsStartOfNewLine() )
134 {
135 OnNewLine();
136
137 lastSpace = NULL;
138 line.clear();
139 lineStart = p;
140 }
141
142 if ( *p == _T('\n') || *p == _T('\0') )
143 {
144 DoOutputLine(line);
145
146 if ( *p == _T('\0') )
147 break;
148 }
149 else // not EOL
150 {
151 if ( *p == _T(' ') )
152 lastSpace = p;
153
154 line += *p;
155
156 if ( widthMax >= 0 && lastSpace )
157 {
158 int width;
159 win->GetTextExtent(line, &width, NULL);
160
161 if ( width > widthMax )
162 {
163 // remove the last word from this line
164 line.erase(lastSpace - lineStart, p + 1 - lineStart);
165 DoOutputLine(line);
166
167 // go back to the last word of this line which we didn't
168 // output yet
169 p = lastSpace;
170 }
171 }
172 //else: no wrapping at all or impossible to wrap
173 }
174 }
175 }
176
177 class wxTextSizerWrapper : public wxTextWrapper
178 {
179 public:
180 wxTextSizerWrapper(wxWindow *win)
181 {
182 m_win = win;
183 m_hLine = 0;
184 }
185
186 wxSizer *CreateSizer(const wxString& text, int widthMax)
187 {
188 m_sizer = new wxBoxSizer(wxVERTICAL);
189 Wrap(m_win, text, widthMax);
190 return m_sizer;
191 }
192
193 protected:
194 virtual void OnOutputLine(const wxString& line)
195 {
196 if ( !line.empty() )
197 {
198 m_sizer->Add(new wxStaticText(m_win, wxID_ANY, line));
199 }
200 else // empty line, no need to create a control for it
201 {
202 if ( !m_hLine )
203 m_hLine = m_win->GetCharHeight();
204
205 m_sizer->Add(5, m_hLine);
206 }
207 }
208
209 private:
210 wxWindow *m_win;
211 wxSizer *m_sizer;
212 int m_hLine;
213 };
214
215 wxSizer *wxDialogBase::CreateTextSizer(const wxString& message)
216 {
217 // I admit that this is complete bogus, but it makes
218 // message boxes work for pda screens temporarily..
219 int widthMax = -1;
220 const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
221 if (is_pda)
222 {
223 widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25;
224 }
225
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("&&"));
231
232 wxTextSizerWrapper wrapper(this);
233
234 return wrapper.CreateSizer(text, widthMax);
235 }
236
237 class wxLabelWrapper : public wxTextWrapper
238 {
239 public:
240 void WrapLabel(wxWindow *text, int widthMax)
241 {
242 m_text.clear();
243 Wrap(text, text->GetLabel(), widthMax);
244 text->SetLabel(m_text);
245 }
246
247 protected:
248 virtual void OnOutputLine(const wxString& line)
249 {
250 m_text += line;
251 }
252
253 virtual void OnNewLine()
254 {
255 m_text += _T('\n');
256 }
257
258 private:
259 wxString m_text;
260 };
261
262 void
263 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
264 wxStaticText
265 #else
266 wxStaticTextBase
267 #endif
268 ::Wrap(int width)
269 {
270 wxLabelWrapper wrapper;
271 wrapper.WrapLabel(this, width);
272 }
273
274 #endif // wxUSE_STATTEXT
275
276 #if wxUSE_BUTTON
277
278 wxSizer *wxDialogBase::CreateButtonSizer( long flags )
279 {
280 #ifdef __SMARTPHONE__
281 wxDialog* dialog = (wxDialog*) this;
282 if (flags & wxOK){
283 dialog->SetLeftMenu(wxID_OK);
284 }
285
286 if (flags & wxCANCEL){
287 dialog->SetRightMenu(wxID_CANCEL);
288 }
289
290 if (flags & wxYES){
291 dialog->SetLeftMenu(wxID_YES);
292 }
293
294 if (flags & wxNO){
295 dialog->SetLeftMenu(wxID_NO);
296 }
297 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
298 return sizer;
299 #else
300 return CreateStdDialogButtonSizer( flags );
301 #endif
302 }
303
304 wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags )
305 {
306 wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer();
307 wxButton *ok = NULL;
308 wxButton *yes = NULL;
309 wxButton *no = NULL;
310
311 if (flags & wxOK){
312 ok = new wxButton(this, wxID_OK);
313 sizer->AddButton(ok);
314 }
315
316 if (flags & wxCANCEL){
317 wxButton *cancel = new wxButton(this, wxID_CANCEL);
318 sizer->AddButton(cancel);
319 }
320
321 if (flags & wxYES){
322 yes = new wxButton(this, wxID_YES);
323 sizer->AddButton(yes);
324 }
325
326 if (flags & wxNO){
327 no = new wxButton(this, wxID_NO);
328 sizer->AddButton(no);
329 }
330
331 if (flags & wxHELP){
332 wxButton *help = new wxButton(this, wxID_HELP);
333 sizer->AddButton(help);
334 }
335
336 if (flags & wxNO_DEFAULT)
337 {
338 if (no)
339 {
340 no->SetDefault();
341 no->SetFocus();
342 }
343 }
344 else
345 {
346 if (ok)
347 {
348 ok->SetDefault();
349 ok->SetFocus();
350 }
351 else if (yes)
352 {
353 yes->SetDefault();
354 yes->SetFocus();
355 }
356 }
357
358 if (flags & wxOK)
359 SetAffirmativeId(wxID_OK);
360 else if (flags & wxYES)
361 SetAffirmativeId(wxID_YES);
362
363 sizer->Realize();
364
365 return sizer;
366 }
367
368
369 #endif // wxUSE_BUTTON