]>
Commit | Line | Data |
---|---|---|
32b8ec41 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Program: wxWindows Widgets Sample | |
3 | // Name: textctrl.cpp | |
4 | // Purpose: part of the widgets sample showing wxTextCtrl | |
5 | // Author: Vadim Zeitlin | |
6 | // Created: 27.03.01 | |
7 | // Id: $Id$ | |
8 | // Copyright: (c) 2001 Vadim Zeitlin | |
9 | // License: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx/wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | // for all others, include the necessary headers | |
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/log.h" | |
30 | #include "wx/timer.h" | |
31 | ||
3bb70c40 | 32 | #include "wx/bitmap.h" |
32b8ec41 VZ |
33 | #include "wx/button.h" |
34 | #include "wx/checkbox.h" | |
35 | #include "wx/radiobox.h" | |
36 | #include "wx/statbox.h" | |
37 | #include "wx/stattext.h" | |
38 | #include "wx/textctrl.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/sizer.h" | |
42 | ||
43 | #include "widgets.h" | |
44 | ||
45 | #include "icons/text.xpm" | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // constants | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | // control ids | |
52 | enum | |
53 | { | |
54 | TextPage_Reset = 100, | |
55 | ||
56 | TextPage_Set, | |
57 | TextPage_Add, | |
58 | TextPage_Insert, | |
59 | TextPage_Clear, | |
60 | TextPage_Load, | |
61 | ||
62 | TextPage_Password, | |
63 | TextPage_WrapLines, | |
64 | TextPage_Textctrl | |
65 | }; | |
66 | ||
67 | // textctrl line number radiobox values | |
68 | enum TextLines | |
69 | { | |
70 | TextLines_Single, | |
71 | TextLines_Multi | |
72 | }; | |
73 | ||
64b54de8 VZ |
74 | #ifdef __WXMSW__ |
75 | ||
76 | // textctrl kind values | |
77 | enum TextKind | |
78 | { | |
79 | TextKind_Plain, | |
80 | TextKind_Rich, | |
81 | TextKind_Rich2 | |
82 | }; | |
83 | ||
84 | #endif // __WXMSW__ | |
85 | ||
32b8ec41 VZ |
86 | // default values for the controls |
87 | static const struct ControlValues | |
88 | { | |
89 | TextLines textLines; | |
90 | bool password; | |
91 | bool wraplines; | |
92 | bool readonly; | |
64b54de8 VZ |
93 | #ifdef __WXMSW__ |
94 | TextKind textKind; | |
95 | #endif // __WXMSW__ | |
32b8ec41 VZ |
96 | } DEFAULTS = |
97 | { | |
98 | TextLines_Multi, // multiline | |
206d3a16 JS |
99 | false, // not password |
100 | true, // do wrap lines | |
101 | false, // not readonly | |
64b54de8 VZ |
102 | #ifdef __WXMSW__ |
103 | TextKind_Plain // plain EDIT control | |
104 | #endif // __WXMSW__ | |
32b8ec41 VZ |
105 | }; |
106 | ||
107 | // ---------------------------------------------------------------------------- | |
108 | // TextWidgetsPage | |
109 | // ---------------------------------------------------------------------------- | |
110 | ||
111 | // Define a new frame type: this is going to be our main frame | |
112 | class TextWidgetsPage : public WidgetsPage | |
113 | { | |
114 | public: | |
115 | // ctor(s) and dtor | |
116 | TextWidgetsPage(wxNotebook *notebook, wxImageList *imaglist); | |
117 | virtual ~TextWidgetsPage(); | |
118 | ||
119 | protected: | |
120 | // create an info text contorl | |
121 | wxTextCtrl *CreateInfoText(); | |
122 | ||
123 | // create a horz sizer holding a static text and this text control | |
124 | wxSizer *CreateTextWithLabelSizer(const wxString& label, | |
125 | wxTextCtrl *text, | |
126 | const wxString& label2 = wxEmptyString, | |
127 | wxTextCtrl *text2 = NULL); | |
128 | ||
129 | // event handlers | |
130 | void OnButtonReset(wxCommandEvent& event); | |
131 | void OnButtonClearLog(wxCommandEvent& event); | |
132 | ||
133 | void OnButtonSet(wxCommandEvent& event); | |
134 | void OnButtonAdd(wxCommandEvent& event); | |
135 | void OnButtonInsert(wxCommandEvent& event); | |
136 | void OnButtonClear(wxCommandEvent& event); | |
137 | void OnButtonLoad(wxCommandEvent& event); | |
138 | ||
139 | void OnButtonQuit(wxCommandEvent& event); | |
140 | ||
141 | void OnText(wxCommandEvent& event); | |
142 | void OnTextEnter(wxCommandEvent& event); | |
143 | ||
144 | void OnCheckOrRadioBox(wxCommandEvent& event); | |
145 | ||
146 | void OnUpdateUIClearButton(wxUpdateUIEvent& event); | |
147 | ||
148 | void OnUpdateUIPasswordCheckbox(wxUpdateUIEvent& event); | |
149 | void OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent& event); | |
150 | ||
151 | void OnUpdateUIResetButton(wxUpdateUIEvent& event); | |
152 | ||
153 | void OnIdle(wxIdleEvent& event); | |
154 | ||
155 | // reset the textctrl parameters | |
156 | void Reset(); | |
157 | ||
158 | // (re)create the textctrl | |
159 | void CreateText(); | |
160 | ||
161 | // is the control currently single line? | |
162 | bool IsSingleLine() const | |
163 | { | |
164 | return m_radioTextLines->GetSelection() == TextLines_Single; | |
165 | } | |
166 | ||
167 | // the controls | |
168 | // ------------ | |
169 | ||
170 | // the radiobox to choose between single and multi line | |
171 | wxRadioBox *m_radioTextLines; | |
172 | ||
173 | // the checkboxes controlling text ctrl styles | |
174 | wxCheckBox *m_chkPassword, | |
175 | *m_chkWrapLines, | |
176 | *m_chkReadonly; | |
177 | ||
64b54de8 VZ |
178 | // under MSW we test rich edit controls as well here |
179 | #ifdef __WXMSW__ | |
180 | wxRadioBox *m_radioKind; | |
181 | #endif // __WXMSW__ | |
182 | ||
32b8ec41 VZ |
183 | // the textctrl itself and the sizer it is in |
184 | wxTextCtrl *m_text; | |
185 | wxSizer *m_sizerText; | |
186 | ||
187 | // the information text zones | |
188 | wxTextCtrl *m_textPosCur, | |
189 | *m_textRowCur, | |
190 | *m_textColCur, | |
191 | *m_textPosLast, | |
192 | *m_textLineLast, | |
193 | *m_textSelFrom, | |
64b54de8 VZ |
194 | *m_textSelTo, |
195 | *m_textRange; | |
32b8ec41 VZ |
196 | |
197 | // and the data to show in them | |
198 | long m_posCur, | |
199 | m_posLast, | |
200 | m_selFrom, | |
201 | m_selTo; | |
202 | ||
626117f7 VZ |
203 | wxString m_range10_20; |
204 | ||
32b8ec41 VZ |
205 | private: |
206 | // any class wishing to process wxWindows events must use this macro | |
5e173f35 GD |
207 | DECLARE_EVENT_TABLE() |
208 | DECLARE_WIDGETS_PAGE(TextWidgetsPage) | |
32b8ec41 VZ |
209 | }; |
210 | ||
efe66bbc VZ |
211 | // ---------------------------------------------------------------------------- |
212 | // WidgetsTextCtrl | |
213 | // ---------------------------------------------------------------------------- | |
214 | ||
215 | class WidgetsTextCtrl : public wxTextCtrl | |
216 | { | |
217 | public: | |
218 | WidgetsTextCtrl(wxWindow *parent, | |
219 | wxWindowID id, | |
220 | const wxString& value, | |
221 | int flags) | |
222 | : wxTextCtrl(parent, id, value, wxDefaultPosition, wxDefaultSize, flags) | |
223 | { | |
224 | } | |
225 | ||
226 | protected: | |
227 | void OnRightClick(wxMouseEvent& event) | |
228 | { | |
229 | wxString where; | |
230 | wxTextCoord x, y; | |
231 | switch ( HitTest(event.GetPosition(), &x, &y) ) | |
232 | { | |
233 | default: | |
234 | wxFAIL_MSG( _T("unexpected HitTest() result") ); | |
235 | // fall through | |
236 | ||
237 | case wxTE_HT_UNKNOWN: | |
238 | x = y = -1; | |
239 | where = _T("nowhere near"); | |
240 | break; | |
241 | ||
242 | case wxTE_HT_BEFORE: | |
243 | where = _T("before"); | |
244 | break; | |
245 | ||
246 | case wxTE_HT_BELOW: | |
247 | where = _T("below"); | |
248 | break; | |
249 | ||
250 | case wxTE_HT_BEYOND: | |
251 | where = _T("beyond"); | |
252 | break; | |
253 | ||
254 | case wxTE_HT_ON_TEXT: | |
255 | where = _T("at"); | |
256 | break; | |
257 | } | |
258 | ||
259 | wxLogMessage(_T("Mouse is %s (%ld, %ld)"), where.c_str(), x, y); | |
260 | ||
261 | event.Skip(); | |
262 | } | |
263 | ||
264 | private: | |
265 | DECLARE_EVENT_TABLE() | |
266 | }; | |
267 | ||
32b8ec41 VZ |
268 | // ---------------------------------------------------------------------------- |
269 | // event tables | |
270 | // ---------------------------------------------------------------------------- | |
271 | ||
272 | BEGIN_EVENT_TABLE(TextWidgetsPage, WidgetsPage) | |
273 | EVT_IDLE(TextWidgetsPage::OnIdle) | |
274 | ||
275 | EVT_BUTTON(TextPage_Reset, TextWidgetsPage::OnButtonReset) | |
276 | ||
277 | EVT_BUTTON(TextPage_Clear, TextWidgetsPage::OnButtonClear) | |
278 | EVT_BUTTON(TextPage_Set, TextWidgetsPage::OnButtonSet) | |
279 | EVT_BUTTON(TextPage_Add, TextWidgetsPage::OnButtonAdd) | |
280 | EVT_BUTTON(TextPage_Insert, TextWidgetsPage::OnButtonInsert) | |
281 | EVT_BUTTON(TextPage_Load, TextWidgetsPage::OnButtonLoad) | |
282 | ||
283 | EVT_UPDATE_UI(TextPage_Clear, TextWidgetsPage::OnUpdateUIClearButton) | |
284 | ||
285 | EVT_UPDATE_UI(TextPage_Password, TextWidgetsPage::OnUpdateUIPasswordCheckbox) | |
286 | EVT_UPDATE_UI(TextPage_WrapLines, TextWidgetsPage::OnUpdateUIWrapLinesCheckbox) | |
287 | ||
288 | EVT_UPDATE_UI(TextPage_Reset, TextWidgetsPage::OnUpdateUIResetButton) | |
289 | ||
290 | EVT_TEXT(TextPage_Textctrl, TextWidgetsPage::OnText) | |
291 | EVT_TEXT_ENTER(TextPage_Textctrl, TextWidgetsPage::OnTextEnter) | |
292 | ||
206d3a16 JS |
293 | EVT_CHECKBOX(wxID_ANY, TextWidgetsPage::OnCheckOrRadioBox) |
294 | EVT_RADIOBOX(wxID_ANY, TextWidgetsPage::OnCheckOrRadioBox) | |
32b8ec41 VZ |
295 | END_EVENT_TABLE() |
296 | ||
efe66bbc VZ |
297 | BEGIN_EVENT_TABLE(WidgetsTextCtrl, wxTextCtrl) |
298 | EVT_RIGHT_UP(WidgetsTextCtrl::OnRightClick) | |
299 | END_EVENT_TABLE() | |
300 | ||
32b8ec41 VZ |
301 | // ============================================================================ |
302 | // implementation | |
303 | // ============================================================================ | |
304 | ||
305 | IMPLEMENT_WIDGETS_PAGE(TextWidgetsPage, _T("Text")); | |
306 | ||
307 | // ---------------------------------------------------------------------------- | |
308 | // TextWidgetsPage creation | |
309 | // ---------------------------------------------------------------------------- | |
310 | ||
311 | TextWidgetsPage::TextWidgetsPage(wxNotebook *notebook, wxImageList *imaglist) | |
312 | : WidgetsPage(notebook) | |
313 | { | |
314 | imaglist->Add(wxBitmap(text_xpm)); | |
315 | ||
316 | // init everything | |
64b54de8 VZ |
317 | #ifdef __WXMSW__ |
318 | m_radioKind = | |
319 | #endif // __WXMSW__ | |
32b8ec41 VZ |
320 | m_radioTextLines = (wxRadioBox *)NULL; |
321 | ||
322 | m_chkPassword = | |
323 | m_chkWrapLines = | |
324 | m_chkReadonly = (wxCheckBox *)NULL; | |
325 | ||
326 | m_text = | |
327 | m_textPosCur = | |
328 | m_textRowCur = | |
329 | m_textColCur = | |
330 | m_textPosLast = | |
331 | m_textLineLast = | |
332 | m_textSelFrom = | |
64b54de8 VZ |
333 | m_textSelTo = |
334 | m_textRange = (wxTextCtrl *)NULL; | |
335 | ||
32b8ec41 VZ |
336 | m_sizerText = (wxSizer *)NULL; |
337 | ||
338 | m_posCur = | |
339 | m_posLast = | |
340 | m_selFrom = | |
341 | m_selTo = -2; // not -1 which means "no selection" | |
342 | ||
343 | // left pane | |
344 | static const wxString modes[] = | |
345 | { | |
346 | _T("single line"), | |
347 | _T("multi line"), | |
348 | }; | |
349 | ||
206d3a16 JS |
350 | wxStaticBox *box = new wxStaticBox(this, wxID_ANY, _T("&Set textctrl parameters")); |
351 | m_radioTextLines = new wxRadioBox(this, wxID_ANY, _T("&Number of lines:"), | |
32b8ec41 VZ |
352 | wxDefaultPosition, wxDefaultSize, |
353 | WXSIZEOF(modes), modes, | |
354 | 1, wxRA_SPECIFY_COLS); | |
355 | ||
356 | wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL); | |
357 | ||
358 | sizerLeft->Add(m_radioTextLines, 0, wxGROW | wxALL, 5); | |
359 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
360 | ||
361 | m_chkPassword = CreateCheckBoxAndAddToSizer( | |
362 | sizerLeft, _T("&Password control"), TextPage_Password | |
363 | ); | |
364 | m_chkWrapLines = CreateCheckBoxAndAddToSizer( | |
365 | sizerLeft, _T("Line &wrap"), TextPage_WrapLines | |
366 | ); | |
367 | m_chkReadonly = CreateCheckBoxAndAddToSizer( | |
368 | sizerLeft, _T("&Read-only mode") | |
369 | ); | |
370 | ||
64b54de8 VZ |
371 | #ifdef __WXMSW__ |
372 | static const wxString kinds[] = | |
373 | { | |
374 | _T("plain edit"), | |
375 | _T("rich edit"), | |
376 | _T("rich edit 2.0"), | |
377 | }; | |
378 | ||
206d3a16 | 379 | m_radioKind = new wxRadioBox(this, wxID_ANY, _T("Control &kind"), |
64b54de8 VZ |
380 | wxDefaultPosition, wxDefaultSize, |
381 | WXSIZEOF(kinds), kinds, | |
382 | 1, wxRA_SPECIFY_COLS); | |
383 | ||
384 | sizerLeft->Add(5, 5, 0, wxGROW | wxALL, 5); // spacer | |
385 | sizerLeft->Add(m_radioKind, 0, wxGROW | wxALL, 5); | |
386 | #endif // __WXMSW__ | |
387 | ||
32b8ec41 | 388 | wxButton *btn = new wxButton(this, TextPage_Reset, _T("&Reset")); |
7b127900 | 389 | sizerLeft->Add(2, 2, 0, wxGROW | wxALL, 1); // spacer |
32b8ec41 VZ |
390 | sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15); |
391 | ||
392 | // middle pane | |
206d3a16 | 393 | wxStaticBox *box2 = new wxStaticBox(this, wxID_ANY, _T("&Change contents:")); |
32b8ec41 VZ |
394 | wxSizer *sizerMiddleUp = new wxStaticBoxSizer(box2, wxVERTICAL); |
395 | ||
396 | btn = new wxButton(this, TextPage_Set, _T("&Set text value")); | |
7b127900 | 397 | sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1); |
32b8ec41 VZ |
398 | |
399 | btn = new wxButton(this, TextPage_Add, _T("&Append text")); | |
7b127900 | 400 | sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1); |
32b8ec41 VZ |
401 | |
402 | btn = new wxButton(this, TextPage_Insert, _T("&Insert text")); | |
7b127900 | 403 | sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1); |
32b8ec41 VZ |
404 | |
405 | btn = new wxButton(this, TextPage_Load, _T("&Load file")); | |
7b127900 | 406 | sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1); |
32b8ec41 VZ |
407 | |
408 | btn = new wxButton(this, TextPage_Clear, _T("&Clear")); | |
7b127900 | 409 | sizerMiddleUp->Add(btn, 0, wxALL | wxGROW, 1); |
32b8ec41 | 410 | |
206d3a16 | 411 | wxStaticBox *box4 = new wxStaticBox(this, wxID_ANY, _T("&Info:")); |
32b8ec41 VZ |
412 | wxSizer *sizerMiddleDown = new wxStaticBoxSizer(box4, wxVERTICAL); |
413 | ||
414 | m_textPosCur = CreateInfoText(); | |
415 | m_textRowCur = CreateInfoText(); | |
416 | m_textColCur = CreateInfoText(); | |
417 | ||
418 | wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL); | |
419 | sizerRow->Add(CreateTextWithLabelSizer | |
420 | ( | |
421 | _T("Current pos:"), | |
422 | m_textPosCur | |
423 | ), | |
424 | 0, wxRIGHT, 5); | |
425 | sizerRow->Add(CreateTextWithLabelSizer | |
426 | ( | |
427 | _T("Col:"), | |
428 | m_textColCur | |
429 | ), | |
430 | 0, wxLEFT | wxRIGHT, 5); | |
431 | sizerRow->Add(CreateTextWithLabelSizer | |
432 | ( | |
433 | _T("Row:"), | |
434 | m_textRowCur | |
435 | ), | |
436 | 0, wxLEFT, 5); | |
437 | sizerMiddleDown->Add(sizerRow, 0, wxALL, 5); | |
438 | ||
439 | m_textLineLast = CreateInfoText(); | |
440 | m_textPosLast = CreateInfoText(); | |
441 | sizerMiddleDown->Add | |
442 | ( | |
443 | CreateTextWithLabelSizer | |
444 | ( | |
445 | _T("Number of lines:"), | |
446 | m_textLineLast, | |
447 | _T("Last position:"), | |
448 | m_textPosLast | |
449 | ), | |
450 | 0, wxALL, 5 | |
451 | ); | |
452 | ||
453 | m_textSelFrom = CreateInfoText(); | |
454 | m_textSelTo = CreateInfoText(); | |
455 | sizerMiddleDown->Add | |
456 | ( | |
457 | CreateTextWithLabelSizer | |
458 | ( | |
459 | _T("Selection: from"), | |
460 | m_textSelFrom, | |
461 | _T("to"), | |
462 | m_textSelTo | |
463 | ), | |
464 | 0, wxALL, 5 | |
465 | ); | |
64b54de8 | 466 | |
206d3a16 | 467 | m_textRange = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
64b54de8 VZ |
468 | wxDefaultPosition, wxDefaultSize, |
469 | wxTE_READONLY); | |
470 | sizerMiddleDown->Add | |
471 | ( | |
472 | CreateTextWithLabelSizer | |
473 | ( | |
474 | _T("Range 10..20:"), | |
475 | m_textRange | |
476 | ), | |
477 | 0, wxALL, 5 | |
478 | ); | |
479 | ||
32b8ec41 VZ |
480 | wxSizer *sizerMiddle = new wxBoxSizer(wxVERTICAL); |
481 | sizerMiddle->Add(sizerMiddleUp, 0, wxGROW); | |
482 | sizerMiddle->Add(sizerMiddleDown, 1, wxGROW | wxTOP, 5); | |
483 | ||
484 | // right pane | |
206d3a16 | 485 | wxStaticBox *box3 = new wxStaticBox(this, wxID_ANY, _T("&Text:")); |
32b8ec41 VZ |
486 | m_sizerText = new wxStaticBoxSizer(box3, wxHORIZONTAL); |
487 | Reset(); | |
488 | CreateText(); | |
7b127900 | 489 | m_sizerText->SetMinSize(150, 0); |
32b8ec41 VZ |
490 | |
491 | // the 3 panes panes compose the upper part of the window | |
492 | wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL); | |
493 | sizerTop->Add(sizerLeft, 0, wxGROW | (wxALL & ~wxLEFT), 10); | |
494 | sizerTop->Add(sizerMiddle, 0, wxGROW | wxALL, 10); | |
495 | sizerTop->Add(m_sizerText, 1, wxGROW | (wxALL & ~wxRIGHT), 10); | |
496 | ||
32b8ec41 VZ |
497 | SetSizer(sizerTop); |
498 | ||
499 | sizerTop->Fit(this); | |
500 | } | |
501 | ||
502 | TextWidgetsPage::~TextWidgetsPage() | |
503 | { | |
504 | } | |
505 | ||
506 | // ---------------------------------------------------------------------------- | |
507 | // creation helpers | |
508 | // ---------------------------------------------------------------------------- | |
509 | ||
510 | wxTextCtrl *TextWidgetsPage::CreateInfoText() | |
511 | { | |
512 | static int s_maxWidth = 0; | |
513 | if ( !s_maxWidth ) | |
514 | { | |
515 | // calc it once only | |
516 | GetTextExtent(_T("9999999"), &s_maxWidth, NULL); | |
517 | } | |
518 | ||
206d3a16 | 519 | wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, wxEmptyString, |
32b8ec41 VZ |
520 | wxDefaultPosition, |
521 | wxSize(s_maxWidth, -1), | |
522 | wxTE_READONLY); | |
523 | return text; | |
524 | } | |
525 | ||
526 | wxSizer *TextWidgetsPage::CreateTextWithLabelSizer(const wxString& label, | |
527 | wxTextCtrl *text, | |
528 | const wxString& label2, | |
529 | wxTextCtrl *text2) | |
530 | { | |
531 | wxSizer *sizerRow = new wxBoxSizer(wxHORIZONTAL); | |
206d3a16 | 532 | sizerRow->Add(new wxStaticText(this, wxID_ANY, label), 0, |
32b8ec41 VZ |
533 | wxALIGN_CENTRE_VERTICAL | wxRIGHT, 5); |
534 | sizerRow->Add(text, 0, wxALIGN_CENTRE_VERTICAL); | |
535 | if ( text2 ) | |
536 | { | |
206d3a16 | 537 | sizerRow->Add(new wxStaticText(this, wxID_ANY, label2), 0, |
32b8ec41 VZ |
538 | wxALIGN_CENTRE_VERTICAL | wxLEFT | wxRIGHT, 5); |
539 | sizerRow->Add(text2, 0, wxALIGN_CENTRE_VERTICAL); | |
540 | } | |
541 | ||
542 | return sizerRow; | |
543 | } | |
544 | ||
545 | // ---------------------------------------------------------------------------- | |
546 | // operations | |
547 | // ---------------------------------------------------------------------------- | |
548 | ||
549 | void TextWidgetsPage::Reset() | |
550 | { | |
551 | m_radioTextLines->SetSelection(DEFAULTS.textLines); | |
552 | m_chkPassword->SetValue(DEFAULTS.password); | |
553 | m_chkWrapLines->SetValue(DEFAULTS.wraplines); | |
554 | m_chkReadonly->SetValue(DEFAULTS.readonly); | |
64b54de8 VZ |
555 | #ifdef __WXMSW__ |
556 | m_radioKind->SetSelection(DEFAULTS.textKind); | |
557 | #endif // __WXMSW__ | |
32b8ec41 VZ |
558 | } |
559 | ||
560 | void TextWidgetsPage::CreateText() | |
561 | { | |
562 | int flags = 0; | |
563 | switch ( m_radioTextLines->GetSelection() ) | |
564 | { | |
565 | default: | |
64b54de8 | 566 | wxFAIL_MSG( _T("unexpected lines radio box selection") ); |
32b8ec41 VZ |
567 | |
568 | case TextLines_Single: | |
569 | break; | |
570 | ||
571 | case TextLines_Multi: | |
572 | flags |= wxTE_MULTILINE; | |
206d3a16 | 573 | m_chkPassword->SetValue(false); |
32b8ec41 VZ |
574 | break; |
575 | } | |
576 | ||
577 | if ( m_chkPassword->GetValue() ) | |
578 | flags |= wxTE_PASSWORD; | |
579 | if ( m_chkReadonly->GetValue() ) | |
580 | flags |= wxTE_READONLY; | |
581 | if ( !m_chkWrapLines->GetValue() ) | |
582 | flags |= wxHSCROLL; | |
583 | ||
64b54de8 VZ |
584 | #ifdef __WXMSW__ |
585 | switch ( m_radioKind->GetSelection() ) | |
586 | { | |
587 | default: | |
588 | wxFAIL_MSG( _T("unexpected kind radio box selection") ); | |
589 | ||
590 | case TextKind_Plain: | |
591 | break; | |
592 | ||
593 | case TextKind_Rich: | |
594 | flags |= wxTE_RICH; | |
595 | break; | |
596 | ||
597 | case TextKind_Rich2: | |
598 | flags |= wxTE_RICH2; | |
599 | break; | |
600 | } | |
601 | #endif // __WXMSW__ | |
602 | ||
32b8ec41 VZ |
603 | wxString valueOld; |
604 | if ( m_text ) | |
605 | { | |
606 | valueOld = m_text->GetValue(); | |
607 | ||
12a3f227 | 608 | m_sizerText->Detach( m_text ); |
32b8ec41 VZ |
609 | delete m_text; |
610 | } | |
611 | else | |
612 | { | |
613 | valueOld = _T("Hello, Universe!"); | |
614 | } | |
615 | ||
efe66bbc | 616 | m_text = new WidgetsTextCtrl(this, TextPage_Textctrl, valueOld, flags); |
72383a72 VZ |
617 | |
618 | // cast to int needed to silence gcc warning about different enums | |
32b8ec41 | 619 | m_sizerText->Add(m_text, 1, wxALL | |
72383a72 | 620 | (flags & wxTE_MULTILINE ? (int)wxGROW |
32b8ec41 VZ |
621 | : wxALIGN_TOP), 5); |
622 | m_sizerText->Layout(); | |
623 | } | |
624 | ||
625 | // ---------------------------------------------------------------------------- | |
626 | // event handlers | |
627 | // ---------------------------------------------------------------------------- | |
628 | ||
629 | void TextWidgetsPage::OnIdle(wxIdleEvent& WXUNUSED(event)) | |
630 | { | |
631 | // update all info texts | |
632 | ||
633 | if ( m_textPosCur ) | |
634 | { | |
635 | long posCur = m_text->GetInsertionPoint(); | |
636 | if ( posCur != m_posCur ) | |
637 | { | |
638 | m_textPosCur->Clear(); | |
639 | m_textRowCur->Clear(); | |
640 | m_textColCur->Clear(); | |
641 | ||
642 | long col, row; | |
643 | m_text->PositionToXY(posCur, &col, &row); | |
644 | ||
645 | *m_textPosCur << posCur; | |
646 | *m_textRowCur << row; | |
647 | *m_textColCur << col; | |
648 | ||
649 | m_posCur = posCur; | |
650 | } | |
651 | } | |
652 | ||
653 | if ( m_textPosLast ) | |
654 | { | |
655 | long posLast = m_text->GetLastPosition(); | |
656 | if ( posLast != m_posLast ) | |
657 | { | |
658 | m_textPosLast->Clear(); | |
659 | *m_textPosLast << posLast; | |
660 | ||
661 | m_posLast = posLast; | |
662 | } | |
663 | } | |
664 | ||
665 | if ( m_textLineLast ) | |
666 | { | |
667 | m_textLineLast->SetValue( | |
aec18ff7 | 668 | wxString::Format(_T("%d"), m_text->GetNumberOfLines()) ); |
32b8ec41 VZ |
669 | } |
670 | ||
671 | if ( m_textSelFrom && m_textSelTo ) | |
672 | { | |
673 | long selFrom, selTo; | |
674 | m_text->GetSelection(&selFrom, &selTo); | |
675 | if ( selFrom != m_selFrom ) | |
676 | { | |
677 | m_textSelFrom->Clear(); | |
678 | *m_textSelFrom << selFrom; | |
679 | ||
680 | m_selFrom = selFrom; | |
681 | } | |
682 | ||
683 | if ( selTo != m_selTo ) | |
684 | { | |
685 | m_textSelTo->Clear(); | |
686 | *m_textSelTo << selTo; | |
687 | ||
688 | m_selTo = selTo; | |
689 | } | |
690 | } | |
64b54de8 VZ |
691 | |
692 | if ( m_textRange ) | |
693 | { | |
626117f7 VZ |
694 | wxString range = m_text->GetRange(10, 20); |
695 | if ( range != m_range10_20 ) | |
696 | { | |
697 | m_range10_20 = range; | |
698 | m_textRange->SetValue(range); | |
699 | } | |
64b54de8 | 700 | } |
32b8ec41 VZ |
701 | } |
702 | ||
703 | void TextWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) | |
704 | { | |
705 | Reset(); | |
706 | ||
707 | CreateText(); | |
708 | } | |
709 | ||
710 | void TextWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event)) | |
711 | { | |
64b54de8 VZ |
712 | m_text->SetValue(m_text->GetWindowStyle() & wxTE_MULTILINE |
713 | ? _T("Here,\nthere and\neverywhere") | |
714 | : _T("Yellow submarine")); | |
715 | ||
32b8ec41 VZ |
716 | m_text->SetFocus(); |
717 | } | |
718 | ||
719 | void TextWidgetsPage::OnButtonAdd(wxCommandEvent& WXUNUSED(event)) | |
720 | { | |
64b54de8 VZ |
721 | if ( m_text->GetWindowStyle() & wxTE_MULTILINE ) |
722 | { | |
723 | m_text->AppendText(_T("We all live in a\n")); | |
724 | } | |
725 | ||
726 | m_text->AppendText(_T("Yellow submarine")); | |
32b8ec41 VZ |
727 | } |
728 | ||
729 | void TextWidgetsPage::OnButtonInsert(wxCommandEvent& WXUNUSED(event)) | |
730 | { | |
64b54de8 VZ |
731 | m_text->WriteText(_T("Is there anybody going to listen to my story")); |
732 | if ( m_text->GetWindowStyle() & wxTE_MULTILINE ) | |
733 | { | |
734 | m_text->WriteText(_T("\nall about the girl who came to stay")); | |
735 | } | |
32b8ec41 VZ |
736 | } |
737 | ||
738 | void TextWidgetsPage::OnButtonClear(wxCommandEvent& WXUNUSED(event)) | |
739 | { | |
740 | m_text->Clear(); | |
741 | m_text->SetFocus(); | |
742 | } | |
743 | ||
744 | void TextWidgetsPage::OnButtonLoad(wxCommandEvent& WXUNUSED(event)) | |
745 | { | |
746 | // search for the file in several dirs where it's likely to be | |
747 | wxPathList pathlist; | |
748 | pathlist.Add(_T(".")); | |
749 | pathlist.Add(_T("..")); | |
750 | pathlist.Add(_T("../../../samples/widgets")); | |
751 | ||
752 | wxString filename = pathlist.FindValidPath(_T("textctrl.cpp")); | |
753 | if ( !filename ) | |
754 | { | |
755 | wxLogError(_T("File textctrl.cpp not found.")); | |
756 | } | |
757 | else // load it | |
758 | { | |
759 | wxStopWatch sw; | |
760 | if ( !m_text->LoadFile(filename) ) | |
761 | { | |
762 | // this is not supposed to happen ... | |
763 | wxLogError(_T("Error loading file.")); | |
764 | } | |
765 | else | |
766 | { | |
767 | long elapsed = sw.Time(); | |
aec18ff7 | 768 | wxLogMessage(_T("Loaded file '%s' in %lu.%us"), |
8b1d8f36 VZ |
769 | filename.c_str(), elapsed / 1000, |
770 | (unsigned int) elapsed % 1000); | |
32b8ec41 VZ |
771 | } |
772 | } | |
773 | } | |
774 | ||
775 | void TextWidgetsPage::OnUpdateUIClearButton(wxUpdateUIEvent& event) | |
776 | { | |
777 | event.Enable(!m_text->GetValue().empty()); | |
778 | } | |
779 | ||
780 | void TextWidgetsPage::OnUpdateUIWrapLinesCheckbox(wxUpdateUIEvent& event) | |
781 | { | |
782 | event.Enable( !IsSingleLine() ); | |
783 | } | |
784 | ||
785 | void TextWidgetsPage::OnUpdateUIPasswordCheckbox(wxUpdateUIEvent& event) | |
786 | { | |
787 | // can't put multiline control in password mode | |
788 | event.Enable( IsSingleLine() ); | |
789 | } | |
790 | ||
791 | void TextWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) | |
792 | { | |
793 | event.Enable( (m_radioTextLines->GetSelection() != DEFAULTS.textLines) || | |
64b54de8 VZ |
794 | #ifdef __WXMSW__ |
795 | (m_radioKind->GetSelection() != DEFAULTS.textKind) || | |
796 | #endif // __WXMSW__ | |
32b8ec41 VZ |
797 | (m_chkReadonly->GetValue() != DEFAULTS.readonly) || |
798 | (m_chkPassword->GetValue() != DEFAULTS.password) || | |
799 | (m_chkWrapLines->GetValue() != DEFAULTS.wraplines) ); | |
800 | } | |
801 | ||
c02e5a31 | 802 | void TextWidgetsPage::OnText(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
803 | { |
804 | // small hack to suppress the very first message: by then the logging is | |
805 | // not yet redirected and so initial setting of the text value results in | |
806 | // an annoying message box | |
206d3a16 | 807 | static bool s_firstTime = true; |
32b8ec41 VZ |
808 | if ( s_firstTime ) |
809 | { | |
206d3a16 | 810 | s_firstTime = false; |
32b8ec41 VZ |
811 | return; |
812 | } | |
813 | ||
814 | wxLogMessage(_T("Text ctrl value changed")); | |
815 | } | |
816 | ||
817 | void TextWidgetsPage::OnTextEnter(wxCommandEvent& event) | |
818 | { | |
819 | wxLogMessage(_T("Text entered: '%s'"), event.GetString().c_str()); | |
820 | } | |
821 | ||
c02e5a31 | 822 | void TextWidgetsPage::OnCheckOrRadioBox(wxCommandEvent& WXUNUSED(event)) |
32b8ec41 VZ |
823 | { |
824 | CreateText(); | |
825 | } | |
72383a72 | 826 |