]>
Commit | Line | Data |
---|---|---|
e37feda2 VZ |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
e37feda2 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1b68e0b5 | 21 | #pragma implementation "dialogbase.h" |
e37feda2 VZ |
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 | |
f6bcfd97 | 32 | #include "wx/button.h" |
e37feda2 VZ |
33 | #include "wx/dialog.h" |
34 | #include "wx/dcclient.h" | |
9f3a38fc | 35 | #include "wx/intl.h" |
e37feda2 | 36 | #include "wx/settings.h" |
9f3a38fc | 37 | #include "wx/stattext.h" |
92afa2b1 | 38 | #include "wx/sizer.h" |
f6bcfd97 | 39 | #include "wx/button.h" |
7d9f12f3 | 40 | #include "wx/containr.h" |
e37feda2 VZ |
41 | #endif |
42 | ||
5d1b4919 VZ |
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 | ||
0db7dfb0 VZ |
60 | // we don't need it, but just to avoid compiler warnings |
61 | virtual ~wxTextWrapper() { } | |
62 | ||
5d1b4919 VZ |
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 | // ---------------------------------------------------------------------------- | |
92afa2b1 | 99 | // wxDialogBase |
5d1b4919 | 100 | // ---------------------------------------------------------------------------- |
e37feda2 | 101 | |
5d1b4919 | 102 | // FIXME - temporary hack in absence of wxTopLevelWindow, should be always used |
7d9f12f3 VS |
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; | |
9ceeecb9 JS |
114 | m_affirmativeId = wxID_OK; |
115 | ||
e4b713a2 VZ |
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 | ||
7d9f12f3 VS |
121 | #ifdef wxTopLevelWindowNative // FIXME - temporary hack, should be always used! |
122 | m_container.SetContainerWindow(this); | |
123 | #endif | |
124 | } | |
125 | ||
5d1b4919 | 126 | #if wxUSE_STATTEXT |
1e6feb95 | 127 | |
5d1b4919 | 128 | void wxTextWrapper::Wrap(wxWindow *win, const wxString& text, int widthMax) |
e37feda2 | 129 | { |
5d1b4919 VZ |
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; | |
68379eaf | 158 | |
5d1b4919 VZ |
159 | if ( widthMax >= 0 && lastSpace ) |
160 | { | |
161 | int width; | |
162 | win->GetTextExtent(line, &width, NULL); | |
68379eaf | 163 | |
5d1b4919 VZ |
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 | wxSizer *wxDialogBase::CreateTextSizer(const wxString& message) | |
181 | { | |
2b5f62a0 VZ |
182 | // I admit that this is complete bogus, but it makes |
183 | // message boxes work for pda screens temporarily.. | |
5d1b4919 VZ |
184 | int widthMax = -1; |
185 | const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA; | |
2b5f62a0 VZ |
186 | if (is_pda) |
187 | { | |
5d1b4919 | 188 | widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25; |
2b5f62a0 | 189 | } |
68379eaf | 190 | |
5d1b4919 VZ |
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("&&")); | |
68379eaf | 196 | |
b730516c | 197 | |
5d1b4919 | 198 | class TextSizerWrapper : public wxTextWrapper |
e37feda2 | 199 | { |
5d1b4919 VZ |
200 | public: |
201 | TextSizerWrapper(wxWindow *win) | |
e37feda2 | 202 | { |
5d1b4919 VZ |
203 | m_win = win; |
204 | m_hLine = 0; | |
205 | } | |
206 | ||
207 | wxSizer *CreateSizer(const wxString& text, int widthMax) | |
208 | { | |
209 | m_sizer = new wxBoxSizer(wxVERTICAL); | |
210 | Wrap(m_win, text, widthMax); | |
211 | return m_sizer; | |
212 | } | |
a350a488 | 213 | |
5d1b4919 VZ |
214 | protected: |
215 | virtual void OnOutputLine(const wxString& line) | |
216 | { | |
217 | if ( !line.empty() ) | |
218 | { | |
219 | m_sizer->Add(new wxStaticText(m_win, wxID_ANY, line)); | |
220 | } | |
221 | else // empty line, no need to create a control for it | |
222 | { | |
223 | if ( !m_hLine ) | |
224 | m_hLine = m_win->GetCharHeight(); | |
225 | ||
226 | m_sizer->Add(5, m_hLine); | |
227 | } | |
228 | } | |
a350a488 | 229 | |
5d1b4919 VZ |
230 | private: |
231 | wxWindow *m_win; | |
232 | wxSizer *m_sizer; | |
233 | int m_hLine; | |
234 | }; | |
a350a488 | 235 | |
5d1b4919 | 236 | TextSizerWrapper wrapper(this); |
68379eaf | 237 | |
5d1b4919 VZ |
238 | return wrapper.CreateSizer(text, widthMax); |
239 | } | |
68379eaf | 240 | |
dec48aa5 | 241 | void |
a351409e | 242 | #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__) |
dec48aa5 VZ |
243 | wxStaticText |
244 | #else | |
245 | wxStaticTextBase | |
246 | #endif | |
247 | ::Wrap(int width) | |
5d1b4919 VZ |
248 | { |
249 | class LabelWrapper : public wxTextWrapper | |
250 | { | |
251 | public: | |
dec48aa5 | 252 | void WrapLabel(wxWindow *text, int widthMax) |
5d1b4919 VZ |
253 | { |
254 | m_text.clear(); | |
255 | Wrap(text, text->GetLabel(), widthMax); | |
256 | text->SetLabel(m_text); | |
e37feda2 | 257 | } |
b730516c | 258 | |
5d1b4919 VZ |
259 | protected: |
260 | virtual void OnOutputLine(const wxString& line) | |
261 | { | |
262 | m_text += line; | |
263 | } | |
264 | ||
265 | virtual void OnNewLine() | |
266 | { | |
267 | m_text += _T('\n'); | |
268 | } | |
269 | ||
270 | private: | |
271 | wxString m_text; | |
272 | }; | |
b730516c | 273 | |
5d1b4919 VZ |
274 | LabelWrapper wrapper; |
275 | wrapper.WrapLabel(this, width); | |
e37feda2 | 276 | } |
b730516c | 277 | |
5d1b4919 | 278 | #endif // wxUSE_STATTEXT |
1e6feb95 VZ |
279 | |
280 | #if wxUSE_BUTTON | |
281 | ||
92afa2b1 | 282 | wxSizer *wxDialogBase::CreateButtonSizer( long flags ) |
e37feda2 | 283 | { |
102c0454 JS |
284 | #ifdef __SMARTPHONE__ |
285 | wxDialog* dialog = (wxDialog*) this; | |
286 | if (flags & wxOK){ | |
287 | dialog->SetLeftMenu(wxID_OK); | |
288 | } | |
289 | ||
290 | if (flags & wxCANCEL){ | |
291 | dialog->SetRightMenu(wxID_CANCEL); | |
292 | } | |
293 | ||
294 | if (flags & wxYES){ | |
295 | dialog->SetLeftMenu(wxID_YES); | |
296 | } | |
297 | ||
298 | if (flags & wxNO){ | |
299 | dialog->SetLeftMenu(wxID_NO); | |
300 | } | |
301 | wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL); | |
302 | return sizer; | |
303 | #else | |
acf2ac37 | 304 | return CreateStdDialogButtonSizer( flags ); |
102c0454 | 305 | #endif |
acf2ac37 | 306 | } |
68379eaf | 307 | |
acf2ac37 RR |
308 | wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags ) |
309 | { | |
310 | wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer(); | |
311 | wxButton *ok = NULL; | |
acf2ac37 RR |
312 | wxButton *yes = NULL; |
313 | wxButton *no = NULL; | |
52069700 | 314 | |
acf2ac37 | 315 | if (flags & wxOK){ |
331c1816 | 316 | ok = new wxButton(this, wxID_OK); |
52069700 | 317 | sizer->AddButton(ok); |
2b5f62a0 | 318 | } |
52069700 | 319 | |
acf2ac37 | 320 | if (flags & wxCANCEL){ |
331c1816 | 321 | wxButton *cancel = new wxButton(this, wxID_CANCEL); |
52069700 | 322 | sizer->AddButton(cancel); |
b5b49e42 | 323 | } |
52069700 | 324 | |
acf2ac37 | 325 | if (flags & wxYES){ |
331c1816 | 326 | yes = new wxButton(this, wxID_YES); |
52069700 | 327 | sizer->AddButton(yes); |
e37feda2 | 328 | } |
52069700 | 329 | |
acf2ac37 | 330 | if (flags & wxNO){ |
331c1816 | 331 | no = new wxButton(this, wxID_NO); |
52069700 | 332 | sizer->AddButton(no); |
e37feda2 | 333 | } |
52069700 | 334 | |
acf2ac37 | 335 | if (flags & wxHELP){ |
331c1816 | 336 | wxButton *help = new wxButton(this, wxID_HELP); |
52069700 | 337 | sizer->AddButton(help); |
e37feda2 | 338 | } |
52069700 | 339 | |
718903fe | 340 | sizer->Realize(); |
52069700 | 341 | |
b730516c RD |
342 | if (flags & wxNO_DEFAULT) |
343 | { | |
344 | if (no) | |
345 | { | |
346 | no->SetDefault(); | |
347 | no->SetFocus(); | |
348 | } | |
349 | } | |
350 | else | |
92afa2b1 RR |
351 | { |
352 | if (ok) | |
353 | { | |
354 | ok->SetDefault(); | |
355 | ok->SetFocus(); | |
356 | } | |
357 | else if (yes) | |
358 | { | |
359 | yes->SetDefault(); | |
360 | yes->SetFocus(); | |
361 | } | |
362 | } | |
9ceeecb9 JS |
363 | |
364 | if (flags & wxOK) | |
365 | SetAffirmativeId(wxID_OK); | |
366 | else if (flags & wxYES) | |
367 | SetAffirmativeId(wxID_YES); | |
b730516c | 368 | |
acf2ac37 | 369 | return sizer; |
e37feda2 VZ |
370 | } |
371 | ||
acf2ac37 | 372 | |
1e6feb95 | 373 | #endif // wxUSE_BUTTON |