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