]> git.saurik.com Git - wxWidgets.git/blob - src/common/dlgcmn.cpp
set error to GSOCK_TIMEOUT if the socket timed out (modified and extended patch 1303554)
[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
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);
116
117 #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used!
118 m_container.SetContainerWindow(this);
119 #endif
120 }
121
122 #if wxUSE_STATTEXT
123
124 void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax)
125 {
126 const wxChar *lastSpace = NULL;
127 wxString line;
128
129 const wxChar *lineStart = text.c_str();
130 for ( const wxChar *p = lineStart; ; p++ )
131 {
132 if ( IsStartOfNewLine() )
133 {
134 OnNewLine();
135
136 lastSpace = NULL;
137 line.clear();
138 lineStart = p;
139 }
140
141 if ( *p == _T('\n') || *p == _T('\0') )
142 {
143 DoOutputLine(line);
144
145 if ( *p == _T('\0') )
146 break;
147 }
148 else // not EOL
149 {
150 if ( *p == _T(' ') )
151 lastSpace = p;
152
153 line += *p;
154
155 if ( widthMax >= 0 && lastSpace )
156 {
157 int width;
158 win->GetTextExtent(line, &width, NULL);
159
160 if ( width > widthMax )
161 {
162 // remove the last word from this line
163 line.erase(lastSpace - lineStart, p + 1 - lineStart);
164 DoOutputLine(line);
165
166 // go back to the last word of this line which we didn't
167 // output yet
168 p = lastSpace;
169 }
170 }
171 //else: no wrapping at all or impossible to wrap
172 }
173 }
174 }
175
176 class wxTextSizerWrapper : public wxTextWrapper
177 {
178 public:
179 wxTextSizerWrapper(wxWindow *win)
180 {
181 m_win = win;
182 m_hLine = 0;
183 }
184
185 wxSizer *CreateSizer(const wxString& text, int widthMax)
186 {
187 m_sizer = new wxBoxSizer(wxVERTICAL);
188 Wrap(m_win, text, widthMax);
189 return m_sizer;
190 }
191
192 protected:
193 virtual void OnOutputLine(const wxString& line)
194 {
195 if ( !line.empty() )
196 {
197 m_sizer->Add(new wxStaticText(m_win, wxID_ANY, line));
198 }
199 else // empty line, no need to create a control for it
200 {
201 if ( !m_hLine )
202 m_hLine = m_win->GetCharHeight();
203
204 m_sizer->Add(5, m_hLine);
205 }
206 }
207
208 private:
209 wxWindow *m_win;
210 wxSizer *m_sizer;
211 int m_hLine;
212 };
213
214 wxSizer *wxDialogBase::CreateTextSizer(const wxString& message)
215 {
216 // I admit that this is complete bogus, but it makes
217 // message boxes work for pda screens temporarily..
218 int widthMax = -1;
219 const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA;
220 if (is_pda)
221 {
222 widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25;
223 }
224
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("&&"));
230
231 wxTextSizerWrapper wrapper(this);
232
233 return wrapper.CreateSizer(text, widthMax);
234 }
235
236 class wxLabelWrapper : public wxTextWrapper
237 {
238 public:
239 void WrapLabel(wxWindow *text, int widthMax)
240 {
241 m_text.clear();
242 Wrap(text, text->GetLabel(), widthMax);
243 text->SetLabel(m_text);
244 }
245
246 protected:
247 virtual void OnOutputLine(const wxString& line)
248 {
249 m_text += line;
250 }
251
252 virtual void OnNewLine()
253 {
254 m_text += _T('\n');
255 }
256
257 private:
258 wxString m_text;
259 };
260
261 void
262 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
263 wxStaticText
264 #else
265 wxStaticTextBase
266 #endif
267 ::Wrap(int width)
268 {
269 wxLabelWrapper wrapper;
270 wrapper.WrapLabel(this, width);
271 }
272
273 #endif // wxUSE_STATTEXT
274
275 #if wxUSE_BUTTON
276
277 wxSizer *wxDialogBase::CreateButtonSizer( long flags )
278 {
279 #ifdef __SMARTPHONE__
280 wxDialog* dialog = (wxDialog*) this;
281 if (flags & wxOK){
282 dialog->SetLeftMenu(wxID_OK);
283 }
284
285 if (flags & wxCANCEL){
286 dialog->SetRightMenu(wxID_CANCEL);
287 }
288
289 if (flags & wxYES){
290 dialog->SetLeftMenu(wxID_YES);
291 }
292
293 if (flags & wxNO){
294 dialog->SetLeftMenu(wxID_NO);
295 }
296 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
297 return sizer;
298 #else
299 return CreateStdDialogButtonSizer( flags );
300 #endif
301 }
302
303 wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags )
304 {
305 wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer();
306 wxButton *ok = NULL;
307 wxButton *yes = NULL;
308 wxButton *no = NULL;
309
310 if (flags & wxOK){
311 ok = new wxButton(this, wxID_OK);
312 sizer->AddButton(ok);
313 }
314
315 if (flags & wxCANCEL){
316 wxButton *cancel = new wxButton(this, wxID_CANCEL);
317 sizer->AddButton(cancel);
318 }
319
320 if (flags & wxYES){
321 yes = new wxButton(this, wxID_YES);
322 sizer->AddButton(yes);
323 }
324
325 if (flags & wxNO){
326 no = new wxButton(this, wxID_NO);
327 sizer->AddButton(no);
328 }
329
330 if (flags & wxHELP){
331 wxButton *help = new wxButton(this, wxID_HELP);
332 sizer->AddButton(help);
333 }
334
335 if (flags & wxNO_DEFAULT)
336 {
337 if (no)
338 {
339 no->SetDefault();
340 no->SetFocus();
341 }
342 }
343 else
344 {
345 if (ok)
346 {
347 ok->SetDefault();
348 ok->SetFocus();
349 }
350 else if (yes)
351 {
352 yes->SetDefault();
353 yes->SetFocus();
354 }
355 }
356
357 if (flags & wxOK)
358 SetAffirmativeId(wxID_OK);
359 else if (flags & wxYES)
360 SetAffirmativeId(wxID_YES);
361
362 sizer->Realize();
363
364 return sizer;
365 }
366
367
368 #endif // wxUSE_BUTTON