]> git.saurik.com Git - wxWidgets.git/blame - samples/text/text.cpp
fixed the last of the off-by-one errors (some are refixed, again...)
[wxWidgets.git] / samples / text / text.cpp
CommitLineData
02e8b87f 1/////////////////////////////////////////////////////////////////////////////
4aff28fc 2// Name: text.cpp
02e8b87f
RR
3// Purpose: TextCtrl wxWindows sample
4// Author: Robert Roebling
5// Modified by:
6// RCS-ID: $Id$
4aff28fc 7// Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
02e8b87f
RR
8// Licence: wxWindows license
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
c41ea66a 12 #pragma implementation "text.cpp"
02e8b87f
RR
13#endif
14
15// For compilers that support precompilation, includes "wx/wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#ifndef WX_PRECOMP
23 #include "wx/wx.h"
24#endif
25
26#if wxUSE_CLIPBOARD
27 #include "wx/dataobj.h"
28 #include "wx/clipbrd.h"
29#endif
30
0ddea946 31#if wxUSE_FILE
ae3dd4a5 32 #include "wx/file.h"
0ddea946
VZ
33#endif
34
02e8b87f
RR
35#if wxUSE_TOOLTIPS
36 #include "wx/tooltip.h"
37#endif
38
b6a0d8a2
VZ
39// We test for wxUSE_DRAG_AND_DROP also, because data objects may not be
40// implemented for compilers that can't cope with the OLE parts in
41// wxUSE_DRAG_AND_DROP.
42#if !wxUSE_DRAG_AND_DROP
43 #undef wxUSE_CLIPBOARD
44 #define wxUSE_CLIPBOARD 0
45#endif
46
02e8b87f
RR
47//----------------------------------------------------------------------
48// class definitions
49//----------------------------------------------------------------------
50
51class MyApp: public wxApp
52{
53public:
54 bool OnInit();
55};
56
57// a text ctrl which allows to call different wxTextCtrl functions
58// interactively by pressing function keys in it
59class MyTextCtrl : public wxTextCtrl
60{
61public:
62 MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value,
63 const wxPoint &pos, const wxSize &size, int style = 0)
f152cf5a 64 : wxTextCtrl(parent, id, value, pos, size, style) { m_hasCapture = FALSE; }
02e8b87f
RR
65
66 void OnKeyDown(wxKeyEvent& event);
67 void OnKeyUp(wxKeyEvent& event);
68 void OnChar(wxKeyEvent& event);
9ca03a57 69 void OnText(wxCommandEvent& event);
c57e3339 70 void OnTextURL(wxTextUrlEvent& event);
4aff28fc 71 void OnMouseEvent(wxMouseEvent& event);
02e8b87f 72
f6bcfd97 73 bool m_hasCapture;
f152cf5a 74
02e8b87f
RR
75private:
76 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
77 void LogEvent(const wxChar *name, wxKeyEvent& event) const;
78
79 DECLARE_EVENT_TABLE()
80};
81
82class MyPanel: public wxPanel
83{
84public:
85 MyPanel(wxFrame *frame, int x, int y, int w, int h);
86
b6a0d8a2
VZ
87#if wxUSE_CLIPBOARD
88 void DoPasteFromClipboard();
89 void DoCopyToClipboard();
90#endif // wxUSE_CLIPBOARD
91
92 void DoMoveToEndOfText();
93 void DoMoveToEndOfEntry();
02e8b87f 94
89124b1a
RR
95 void OnSize( wxSizeEvent &event );
96
02e8b87f
RR
97 MyTextCtrl *m_text;
98 MyTextCtrl *m_password;
99 MyTextCtrl *m_enter;
100 MyTextCtrl *m_tab;
02e8b87f 101 MyTextCtrl *m_readonly;
bb69661b 102
02e8b87f
RR
103 MyTextCtrl *m_multitext;
104 MyTextCtrl *m_horizontal;
02e8b87f 105
0ddea946
VZ
106 MyTextCtrl *m_textrich;
107
02e8b87f 108 wxTextCtrl *m_log;
89124b1a
RR
109
110private:
111 DECLARE_EVENT_TABLE()
02e8b87f
RR
112};
113
114class MyFrame: public wxFrame
115{
116public:
b6a0d8a2 117 MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h);
02e8b87f
RR
118
119 void OnQuit(wxCommandEvent& event);
120 void OnAbout(wxCommandEvent& event);
121#if wxUSE_TOOLTIPS
122 void OnSetTooltipDelay(wxCommandEvent& event);
123 void OnToggleTooltips(wxCommandEvent& event);
124#endif // wxUSE_TOOLTIPS
b6a0d8a2
VZ
125
126#if wxUSE_CLIPBOARD
127 void OnPasteFromClipboard( wxCommandEvent &event )
128 { m_panel->DoPasteFromClipboard(); }
129 void OnCopyToClipboard( wxCommandEvent &event )
130 { m_panel->DoCopyToClipboard(); }
131#endif // wxUSE_CLIPBOARD
132
eff00523
VZ
133 void OnAddTextFreeze( wxCommandEvent& event )
134 { DoAddText(true); }
135 void OnAddText( wxCommandEvent& event )
136 { DoAddText(false); }
137
b6a0d8a2
VZ
138 void OnMoveToEndOfText( wxCommandEvent &event )
139 { m_panel->DoMoveToEndOfText(); }
140 void OnMoveToEndOfEntry( wxCommandEvent &event )
141 { m_panel->DoMoveToEndOfEntry(); }
142
22670da4
VZ
143 void OnScrollLineDown(wxCommandEvent& event)
144 {
145 if ( !m_panel->m_textrich->LineDown() )
146 wxLogMessage(_T("Already at the bottom"));
147 }
148
149 void OnScrollLineUp(wxCommandEvent& event)
150 {
151 if ( !m_panel->m_textrich->LineUp() )
152 wxLogMessage(_T("Already at the top"));
153 }
154
155 void OnScrollPageDown(wxCommandEvent& event)
156 {
157 if ( !m_panel->m_textrich->PageDown() )
158 wxLogMessage(_T("Already at the bottom"));
159 }
160
161 void OnScrollPageUp(wxCommandEvent& event)
162 {
163 if ( !m_panel->m_textrich->PageUp() )
164 wxLogMessage(_T("Already at the top"));
165 }
166
e66ad5c6 167 void OnLogClear(wxCommandEvent& event);
0ddea946 168 void OnFileSave(wxCommandEvent& event);
b6a0d8a2
VZ
169 void OnFileLoad(wxCommandEvent& event);
170
f6bcfd97
BP
171 void OnSetEditable(wxCommandEvent& event);
172 void OnSetEnabled(wxCommandEvent& event);
173
02e8b87f
RR
174 void OnIdle( wxIdleEvent& event );
175
176private:
eff00523
VZ
177 void DoAddText(bool freeze)
178 {
179 wxTextCtrl *text = m_panel->m_textrich;
180 if ( freeze )
181 text->Freeze();
182
183 for ( int i = 0; i < 100; i++ )
184 text->AppendText(wxString::Format("Line %i\n", i));
185
186 text->SetInsertionPoint(0);
187
188 if ( freeze )
189 text->Thaw();
190 }
191
b6a0d8a2
VZ
192 MyPanel *m_panel;
193
02e8b87f
RR
194 DECLARE_EVENT_TABLE()
195};
196
197//----------------------------------------------------------------------
198// main()
199//----------------------------------------------------------------------
200
201IMPLEMENT_APP(MyApp)
202
203//----------------------------------------------------------------------
204// MyApp
205//----------------------------------------------------------------------
206
207enum
208{
b6a0d8a2
VZ
209 TEXT_QUIT = 100,
210 TEXT_ABOUT,
211 TEXT_LOAD,
ae3dd4a5 212 TEXT_SAVE,
e66ad5c6 213 TEXT_CLEAR,
b6a0d8a2
VZ
214
215 // clipboard menu
216 TEXT_CLIPBOARD_COPY = 200,
217 TEXT_CLIPBOARD_PASTE,
02e8b87f
RR
218
219 // tooltip menu
b6a0d8a2
VZ
220 TEXT_TOOLTIPS_SETDELAY = 300,
221 TEXT_TOOLTIPS_ENABLE,
222
f6bcfd97 223 // text menu
eff00523
VZ
224 TEXT_ADD_SOME = 400,
225 TEXT_ADD_FREEZE,
226 TEXT_MOVE_ENDTEXT,
f6bcfd97
BP
227 TEXT_MOVE_ENDENTRY,
228 TEXT_SET_EDITABLE,
22670da4
VZ
229 TEXT_SET_ENABLED,
230 TEXT_LINE_DOWN,
231 TEXT_LINE_UP,
232 TEXT_PAGE_DOWN,
233 TEXT_PAGE_UP,
02e8b87f
RR
234};
235
236bool MyApp::OnInit()
237{
238 // Create the main frame window
239 MyFrame *frame = new MyFrame((wxFrame *) NULL,
17665a2b 240 "Text wxWindows sample", 50, 50, 700, 420);
89124b1a 241 frame->SetSizeHints( 500, 400 );
02e8b87f 242
02e8b87f 243 wxMenu *file_menu = new wxMenu;
e66ad5c6
VZ
244 file_menu->Append(TEXT_CLEAR, "&Clear the log\tCtrl-C",
245 "Clear the log window contents");
0ddea946
VZ
246 file_menu->Append(TEXT_SAVE, "&Save file\tCtrl-S",
247 "Save the text control contents to file");
b6a0d8a2
VZ
248 file_menu->Append(TEXT_LOAD, "&Load file\tCtrl-O",
249 "Load the sample file into text control");
250 file_menu->AppendSeparator();
251 file_menu->Append(TEXT_ABOUT, "&About\tAlt-A");
252 file_menu->AppendSeparator();
c41ea66a 253 file_menu->Append(TEXT_QUIT, "E&xit\tAlt-X", "Quit this sample");
02e8b87f
RR
254
255 wxMenuBar *menu_bar = new wxMenuBar( wxMB_DOCKABLE );
256 menu_bar->Append(file_menu, "&File");
257
258#if wxUSE_TOOLTIPS
259 wxMenu *tooltip_menu = new wxMenu;
b6a0d8a2 260 tooltip_menu->Append(TEXT_TOOLTIPS_SETDELAY, "Set &delay\tCtrl-D");
02e8b87f 261 tooltip_menu->AppendSeparator();
b6a0d8a2 262 tooltip_menu->Append(TEXT_TOOLTIPS_ENABLE, "&Toggle tooltips\tCtrl-T",
02e8b87f 263 "enable/disable tooltips", TRUE);
b6a0d8a2 264 tooltip_menu->Check(TEXT_TOOLTIPS_ENABLE, TRUE);
02e8b87f
RR
265 menu_bar->Append(tooltip_menu, "&Tooltips");
266#endif // wxUSE_TOOLTIPS
267
b6a0d8a2
VZ
268#if wxUSE_CLIPBOARD
269 wxMenu *menuClipboard = new wxMenu;
270 menuClipboard->Append(TEXT_CLIPBOARD_COPY, "&Copy\tCtrl-C",
271 "Copy the first line to the clipboard");
272 menuClipboard->Append(TEXT_CLIPBOARD_PASTE, "&Paste\tCtrl-V",
273 "Paste from clipboard to the text control");
274 menu_bar->Append(menuClipboard, "&Clipboard");
275#endif // wxUSE_CLIPBOARD
276
f6bcfd97 277 wxMenu *menuText = new wxMenu;
eff00523
VZ
278 menuText->Append(TEXT_ADD_SOME, "&Append some text\tCtrl-A");
279 menuText->Append(TEXT_ADD_FREEZE, "&Append text with freeze/thaw\tShift-Ctrl-A");
f6bcfd97
BP
280 menuText->Append(TEXT_MOVE_ENDTEXT, "Move cursor to the end of &text");
281 menuText->Append(TEXT_MOVE_ENDENTRY, "Move cursor to the end of &entry");
282 menuText->Append(TEXT_SET_EDITABLE, "Toggle &editable state", "", TRUE);
283 menuText->Append(TEXT_SET_ENABLED, "Toggle e&nabled state", "", TRUE);
284 menuText->Check(TEXT_SET_EDITABLE, TRUE);
285 menuText->Check(TEXT_SET_ENABLED, TRUE);
22670da4
VZ
286 menuText->AppendSeparator();
287 menuText->Append(TEXT_LINE_DOWN, "Scroll text one line down");
288 menuText->Append(TEXT_LINE_UP, "Scroll text one line up");
289 menuText->Append(TEXT_PAGE_DOWN, "Scroll text one page down");
290 menuText->Append(TEXT_PAGE_DOWN, "Scroll text one page up");
f6bcfd97 291 menu_bar->Append(menuText, "&Text");
b6a0d8a2 292
02e8b87f
RR
293 frame->SetMenuBar(menu_bar);
294
295 frame->Show(TRUE);
296
297 SetTopWindow(frame);
298
299 // report success
300 return TRUE;
301}
302
303//----------------------------------------------------------------------
304// MyTextCtrl
305//----------------------------------------------------------------------
306
307BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
308 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
309 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
310 EVT_CHAR(MyTextCtrl::OnChar)
9ca03a57 311 EVT_TEXT(-1, MyTextCtrl::OnText)
c57e3339 312 EVT_TEXT_URL(-1, MyTextCtrl::OnTextURL)
4aff28fc 313 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent)
02e8b87f
RR
314END_EVENT_TABLE()
315
316void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const
317{
318 wxString key;
319 long keycode = event.KeyCode();
02e8b87f
RR
320 {
321 switch ( keycode )
322 {
323 case WXK_BACK: key = "BACK"; break;
324 case WXK_TAB: key = "TAB"; break;
325 case WXK_RETURN: key = "RETURN"; break;
326 case WXK_ESCAPE: key = "ESCAPE"; break;
327 case WXK_SPACE: key = "SPACE"; break;
328 case WXK_DELETE: key = "DELETE"; break;
329 case WXK_START: key = "START"; break;
330 case WXK_LBUTTON: key = "LBUTTON"; break;
331 case WXK_RBUTTON: key = "RBUTTON"; break;
332 case WXK_CANCEL: key = "CANCEL"; break;
333 case WXK_MBUTTON: key = "MBUTTON"; break;
334 case WXK_CLEAR: key = "CLEAR"; break;
335 case WXK_SHIFT: key = "SHIFT"; break;
336 case WXK_ALT: key = "ALT"; break;
337 case WXK_CONTROL: key = "CONTROL"; break;
338 case WXK_MENU: key = "MENU"; break;
339 case WXK_PAUSE: key = "PAUSE"; break;
340 case WXK_CAPITAL: key = "CAPITAL"; break;
341 case WXK_PRIOR: key = "PRIOR"; break;
342 case WXK_NEXT: key = "NEXT"; break;
343 case WXK_END: key = "END"; break;
344 case WXK_HOME: key = "HOME"; break;
345 case WXK_LEFT: key = "LEFT"; break;
346 case WXK_UP: key = "UP"; break;
347 case WXK_RIGHT: key = "RIGHT"; break;
348 case WXK_DOWN: key = "DOWN"; break;
349 case WXK_SELECT: key = "SELECT"; break;
350 case WXK_PRINT: key = "PRINT"; break;
351 case WXK_EXECUTE: key = "EXECUTE"; break;
352 case WXK_SNAPSHOT: key = "SNAPSHOT"; break;
353 case WXK_INSERT: key = "INSERT"; break;
354 case WXK_HELP: key = "HELP"; break;
355 case WXK_NUMPAD0: key = "NUMPAD0"; break;
356 case WXK_NUMPAD1: key = "NUMPAD1"; break;
357 case WXK_NUMPAD2: key = "NUMPAD2"; break;
358 case WXK_NUMPAD3: key = "NUMPAD3"; break;
359 case WXK_NUMPAD4: key = "NUMPAD4"; break;
360 case WXK_NUMPAD5: key = "NUMPAD5"; break;
361 case WXK_NUMPAD6: key = "NUMPAD6"; break;
362 case WXK_NUMPAD7: key = "NUMPAD7"; break;
363 case WXK_NUMPAD8: key = "NUMPAD8"; break;
364 case WXK_NUMPAD9: key = "NUMPAD9"; break;
365 case WXK_MULTIPLY: key = "MULTIPLY"; break;
366 case WXK_ADD: key = "ADD"; break;
367 case WXK_SEPARATOR: key = "SEPARATOR"; break;
368 case WXK_SUBTRACT: key = "SUBTRACT"; break;
369 case WXK_DECIMAL: key = "DECIMAL"; break;
370 case WXK_DIVIDE: key = "DIVIDE"; break;
371 case WXK_F1: key = "F1"; break;
372 case WXK_F2: key = "F2"; break;
373 case WXK_F3: key = "F3"; break;
374 case WXK_F4: key = "F4"; break;
375 case WXK_F5: key = "F5"; break;
376 case WXK_F6: key = "F6"; break;
377 case WXK_F7: key = "F7"; break;
378 case WXK_F8: key = "F8"; break;
379 case WXK_F9: key = "F9"; break;
380 case WXK_F10: key = "F10"; break;
381 case WXK_F11: key = "F11"; break;
382 case WXK_F12: key = "F12"; break;
383 case WXK_F13: key = "F13"; break;
384 case WXK_F14: key = "F14"; break;
385 case WXK_F15: key = "F15"; break;
386 case WXK_F16: key = "F16"; break;
387 case WXK_F17: key = "F17"; break;
388 case WXK_F18: key = "F18"; break;
389 case WXK_F19: key = "F19"; break;
390 case WXK_F20: key = "F20"; break;
391 case WXK_F21: key = "F21"; break;
392 case WXK_F22: key = "F22"; break;
393 case WXK_F23: key = "F23"; break;
394 case WXK_F24: key = "F24"; break;
395 case WXK_NUMLOCK: key = "NUMLOCK"; break;
396 case WXK_SCROLL: key = "SCROLL"; break;
397 case WXK_PAGEUP: key = "PAGEUP"; break;
398 case WXK_PAGEDOWN: key = "PAGEDOWN"; break;
399 case WXK_NUMPAD_SPACE: key = "NUMPAD_SPACE"; break;
400 case WXK_NUMPAD_TAB: key = "NUMPAD_TAB"; break;
401 case WXK_NUMPAD_ENTER: key = "NUMPAD_ENTER"; break;
402 case WXK_NUMPAD_F1: key = "NUMPAD_F1"; break;
403 case WXK_NUMPAD_F2: key = "NUMPAD_F2"; break;
404 case WXK_NUMPAD_F3: key = "NUMPAD_F3"; break;
405 case WXK_NUMPAD_F4: key = "NUMPAD_F4"; break;
406 case WXK_NUMPAD_HOME: key = "NUMPAD_HOME"; break;
407 case WXK_NUMPAD_LEFT: key = "NUMPAD_LEFT"; break;
408 case WXK_NUMPAD_UP: key = "NUMPAD_UP"; break;
409 case WXK_NUMPAD_RIGHT: key = "NUMPAD_RIGHT"; break;
410 case WXK_NUMPAD_DOWN: key = "NUMPAD_DOWN"; break;
411 case WXK_NUMPAD_PRIOR: key = "NUMPAD_PRIOR"; break;
412 case WXK_NUMPAD_PAGEUP: key = "NUMPAD_PAGEUP"; break;
413 case WXK_NUMPAD_PAGEDOWN: key = "NUMPAD_PAGEDOWN"; break;
414 case WXK_NUMPAD_END: key = "NUMPAD_END"; break;
415 case WXK_NUMPAD_BEGIN: key = "NUMPAD_BEGIN"; break;
416 case WXK_NUMPAD_INSERT: key = "NUMPAD_INSERT"; break;
417 case WXK_NUMPAD_DELETE: key = "NUMPAD_DELETE"; break;
418 case WXK_NUMPAD_EQUAL: key = "NUMPAD_EQUAL"; break;
419 case WXK_NUMPAD_MULTIPLY: key = "NUMPAD_MULTIPLY"; break;
420 case WXK_NUMPAD_ADD: key = "NUMPAD_ADD"; break;
421 case WXK_NUMPAD_SEPARATOR: key = "NUMPAD_SEPARATOR"; break;
422 case WXK_NUMPAD_SUBTRACT: key = "NUMPAD_SUBTRACT"; break;
423 case WXK_NUMPAD_DECIMAL: key = "NUMPAD_DECIMAL"; break;
424
4afd7529 425 default:
89124b1a
RR
426 {
427 if ( wxIsprint((int)keycode) )
428 key.Printf( _T("'%c'") , (char)keycode);
429 else
4afd7529
VZ
430 key.Printf( _T("unknown (%ld)"), keycode);
431 }
02e8b87f
RR
432 }
433 }
434
435 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
436 name,
437 key.c_str(),
438 GetChar( event.ControlDown(), _T('C') ),
439 GetChar( event.AltDown(), _T('A') ),
440 GetChar( event.ShiftDown(), _T('S') ),
441 GetChar( event.MetaDown(), _T('M') ) );
02e8b87f
RR
442}
443
c57e3339
VZ
444static wxString GetMouseEventDesc(const wxMouseEvent& ev)
445{
446 // click event
447 wxString button;
448 bool dbl, up;
449 if ( ev.LeftDown() || ev.LeftUp() || ev.LeftDClick() )
450 {
451 button = _T("Left");
452 dbl = ev.LeftDClick();
453 up = ev.LeftUp();
454 }
455 else if ( ev.MiddleDown() || ev.MiddleUp() || ev.MiddleDClick() )
456 {
457 button = _T("Middle");
458 dbl = ev.MiddleDClick();
459 up = ev.MiddleUp();
460 }
461 else if ( ev.RightDown() || ev.RightUp() || ev.RightDClick() )
462 {
463 button = _T("Right");
464 dbl = ev.RightDClick();
465 up = ev.RightUp();
466 }
467 else
468 {
469 return _T("Unknown mouse event");
470 }
471
472 return wxString::Format(_T("%s mouse button %s"),
473 button.c_str(),
474 dbl ? _T("double clicked")
475 : up ? _T("released") : _T("clicked"));
476}
477
4aff28fc
VZ
478void MyTextCtrl::OnMouseEvent(wxMouseEvent& ev)
479{
480 if ( !ev.Moving() )
481 {
482 wxString msg;
483 if ( ev.Entering() )
484 {
485 msg = _T("Mouse entered the window");
486 }
487 else if ( ev.Leaving() )
488 {
489 msg = _T("Mouse left the window");
490 }
491 else
492 {
c57e3339 493 msg = GetMouseEventDesc(ev);
4aff28fc
VZ
494 }
495
496 msg << _T(" at (") << ev.GetX() << _T(", ") << ev.GetY() << _T(") ")
497 << _T("Flags: ")
4ed0aceb
VZ
498 << GetChar( ev.LeftIsDown(), _T('1') )
499 << GetChar( ev.MiddleIsDown(), _T('2') )
500 << GetChar( ev.RightIsDown(), _T('3') )
4aff28fc
VZ
501 << GetChar( ev.ControlDown(), _T('C') )
502 << GetChar( ev.AltDown(), _T('A') )
503 << GetChar( ev.ShiftDown(), _T('S') )
504 << GetChar( ev.MetaDown(), _T('M') );
505
506 wxLogMessage(msg);
507 }
508 //else: we're not interested in mouse move events
4ed0aceb
VZ
509
510 ev.Skip();
4aff28fc
VZ
511}
512
9ca03a57 513void MyTextCtrl::OnText(wxCommandEvent& event)
02e8b87f 514{
9ca03a57 515 MyTextCtrl *win = (MyTextCtrl *)event.GetEventObject();
ae3dd4a5
VZ
516 const wxChar *data = (const wxChar *)(win->GetClientData());
517 if ( data )
518 {
c41ea66a 519 wxLogMessage(_T("Text changed in control '%s'"), data);
ae3dd4a5 520 }
f6bcfd97
BP
521 else
522 {
523 wxLogMessage(_T("Text changed in some control"));
524 }
9ca03a57
VZ
525}
526
c57e3339
VZ
527void MyTextCtrl::OnTextURL(wxTextUrlEvent& event)
528{
529 const wxMouseEvent& ev = event.GetMouseEvent();
530
531 // filter out mouse moves, too many of them
532 if ( ev.Moving() )
533 return;
534
535 long start = event.GetURLStart(),
536 end = event.GetURLEnd();
537
538 wxLogMessage(_T("Mouse event over URL '%s': %s"),
539 GetValue().Mid(start, end - start).c_str(),
540 GetMouseEventDesc(ev).c_str());
541}
542
9ca03a57
VZ
543void MyTextCtrl::OnChar(wxKeyEvent& event)
544{
545 LogEvent( _T("Char"), event);
ae3dd4a5 546
3017f78d
RR
547/* How are we supposed to test wxTE_PROCESS_TAB with this code?
548
b6a0d8a2
VZ
549 if ( event.KeyCode() == WXK_TAB )
550 {
551 WriteText("\t");
552 }
553 else
554 {
555 event.Skip();
556 }
3017f78d
RR
557*/
558 event.Skip();
02e8b87f
RR
559}
560
561void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
562{
ae3dd4a5 563 LogEvent( _T("Key up"), event);
02e8b87f
RR
564
565 event.Skip();
566}
567
568void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
569{
570 switch ( event.KeyCode() )
571 {
572 case WXK_F1:
573 // show current position and text length
574 {
575 long line, column, pos = GetInsertionPoint();
576 PositionToXY(pos, &column, &line);
577
578 wxLogMessage( _T("Current position: %ld\n"
579 "Current line, column: (%ld, %ld)\n"
580 "Number of lines: %ld\n"
581 "Current line length: %ld\n"
f6bcfd97 582 "Total text length: %u (%ld)"),
02e8b87f
RR
583 pos,
584 line, column,
585 GetNumberOfLines(),
586 GetLineLength(line),
f6bcfd97 587 GetValue().length(),
02e8b87f
RR
588 GetLastPosition());
589 }
590 break;
591
592 case WXK_F2:
593 // go to the end
594 SetInsertionPointEnd();
595 break;
596
597 case WXK_F3:
598 // go to position 10
599 SetInsertionPoint(10);
600 break;
4afd7529 601
f152cf5a 602 case WXK_F4:
4afd7529
VZ
603 if (!m_hasCapture)
604 {
f152cf5a 605 wxLogDebug( wxT("Now capturing mouse and events.") );
4afd7529
VZ
606 m_hasCapture = TRUE;
607 CaptureMouse();
608 }
609 else
610 {
f152cf5a 611 wxLogDebug( wxT("Stopped capturing mouse and events.") );
4afd7529
VZ
612 m_hasCapture = TRUE;
613 ReleaseMouse();
614 }
f152cf5a 615 break;
4afd7529
VZ
616
617 case WXK_F5:
618 // insert a blank line
619 WriteText("\n");
ae3dd4a5 620 break;
02e8b87f 621
f6bcfd97
BP
622 case WXK_F6:
623 SetValue("F6 was just pressed.");
624 break;
625
626 case WXK_F7:
627 ShowPosition(10);
628 break;
0ddea946 629 }
02e8b87f 630
f6bcfd97
BP
631 LogEvent( wxT("Key down"), event);
632
02e8b87f
RR
633 event.Skip();
634}
635
636//----------------------------------------------------------------------
637// MyPanel
638//----------------------------------------------------------------------
639
89124b1a
RR
640BEGIN_EVENT_TABLE(MyPanel, wxPanel)
641 EVT_SIZE(MyPanel::OnSize)
642END_EVENT_TABLE()
643
02e8b87f
RR
644MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
645 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
646{
647 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE );
648
89124b1a
RR
649 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
650
651 delete old_log;
02e8b87f
RR
652
653 // single line text controls
654
bb69661b 655 m_text = new MyTextCtrl( this, -1, "Single line.",
f6bcfd97
BP
656 wxPoint(10,10), wxSize(140,-1),
657 wxTE_PROCESS_ENTER);
4ed0aceb
VZ
658 m_text->SetForegroundColour(*wxBLUE);
659 m_text->SetBackgroundColour(*wxLIGHT_GREY);
02e8b87f
RR
660 (*m_text) << " Appended.";
661 m_text->SetInsertionPoint(0);
662 m_text->WriteText( "Prepended. " );
bb69661b
VZ
663
664 m_password = new MyTextCtrl( this, -1, "",
02e8b87f 665 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD );
bb69661b
VZ
666
667 m_readonly = new MyTextCtrl( this, -1, "Read only",
02e8b87f 668 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY );
bb69661b 669
02e8b87f
RR
670 // multi line text controls
671
bb69661b
VZ
672 m_horizontal = new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
673 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL );
f6bcfd97
BP
674
675 // a little hack to use the command line argument for encoding testing
676 if ( wxTheApp->argc == 2 )
677 {
678 switch ( wxTheApp->argv[1][0] )
679 {
680 case '2':
681 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
682 FALSE, "",
683 wxFONTENCODING_ISO8859_2));
684