]> git.saurik.com Git - wxWidgets.git/blob - samples/text/text.cpp
Don't fail to register remaining window classes if
[wxWidgets.git] / samples / text / text.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: text.cpp
3 // Purpose: TextCtrl wxWindows sample
4 // Author: Robert Roebling
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "text.cpp"
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
31 #if wxUSE_FILE
32 #include "wx/file.h"
33 #endif
34
35 #if wxUSE_TOOLTIPS
36 #include "wx/tooltip.h"
37 #endif
38
39 #include "wx/progdlg.h"
40
41 // We test for wxUSE_DRAG_AND_DROP also, because data objects may not be
42 // implemented for compilers that can't cope with the OLE parts in
43 // wxUSE_DRAG_AND_DROP.
44 #if !wxUSE_DRAG_AND_DROP
45 #undef wxUSE_CLIPBOARD
46 #define wxUSE_CLIPBOARD 0
47 #endif
48
49 //----------------------------------------------------------------------
50 // class definitions
51 //----------------------------------------------------------------------
52
53 class MyApp: public wxApp
54 {
55 public:
56 bool OnInit();
57 };
58
59 // a text ctrl which allows to call different wxTextCtrl functions
60 // interactively by pressing function keys in it
61 class MyTextCtrl : public wxTextCtrl
62 {
63 public:
64 MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value,
65 const wxPoint &pos, const wxSize &size, int style = 0)
66 : wxTextCtrl(parent, id, value, pos, size, style)
67 {
68 m_hasCapture = FALSE;
69 }
70
71 void OnKeyDown(wxKeyEvent& event);
72 void OnKeyUp(wxKeyEvent& event);
73 void OnChar(wxKeyEvent& event);
74
75 void OnText(wxCommandEvent& event);
76 void OnTextURL(wxTextUrlEvent& event);
77 void OnTextMaxLen(wxCommandEvent& event);
78
79 void OnMouseEvent(wxMouseEvent& event);
80
81 void OnSetFocus(wxFocusEvent& event);
82 void OnKillFocus(wxFocusEvent& event);
83
84 static bool ms_logKey;
85 static bool ms_logChar;
86 static bool ms_logMouse;
87 static bool ms_logText;
88 static bool ms_logFocus;
89
90 private:
91 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
92 void LogKeyEvent(const wxChar *name, wxKeyEvent& event) const;
93
94 bool m_hasCapture;
95
96 DECLARE_EVENT_TABLE()
97 };
98
99 class MyPanel: public wxPanel
100 {
101 public:
102 MyPanel(wxFrame *frame, int x, int y, int w, int h);
103 virtual ~MyPanel() { delete wxLog::SetActiveTarget(m_logOld); }
104
105 #if wxUSE_CLIPBOARD
106 void DoPasteFromClipboard();
107 void DoCopyToClipboard();
108 #endif // wxUSE_CLIPBOARD
109
110 void DoRemoveText();
111 void DoMoveToEndOfText();
112 void DoMoveToEndOfEntry();
113
114 void OnSize( wxSizeEvent &event );
115
116 MyTextCtrl *m_text;
117 MyTextCtrl *m_password;
118 MyTextCtrl *m_enter;
119 MyTextCtrl *m_tab;
120 MyTextCtrl *m_readonly;
121 MyTextCtrl *m_limited;
122
123 MyTextCtrl *m_multitext;
124 MyTextCtrl *m_horizontal;
125
126 MyTextCtrl *m_textrich;
127
128 wxTextCtrl *m_log;
129
130 wxLog *m_logOld;
131
132 private:
133 // get the currently focused text control or return the default one is no
134 // text ctrl has focus
135 wxTextCtrl *GetFocusedText(wxTextCtrl *textDef);
136
137 DECLARE_EVENT_TABLE()
138 };
139
140 class MyFrame: public wxFrame
141 {
142 public:
143 MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h);
144
145 void OnQuit(wxCommandEvent& event);
146 void OnAbout(wxCommandEvent& event);
147 #if wxUSE_TOOLTIPS
148 void OnSetTooltipDelay(wxCommandEvent& event);
149 void OnToggleTooltips(wxCommandEvent& event);
150 #endif // wxUSE_TOOLTIPS
151
152 #if wxUSE_CLIPBOARD
153 void OnPasteFromClipboard( wxCommandEvent &event )
154 { m_panel->DoPasteFromClipboard(); }
155 void OnCopyToClipboard( wxCommandEvent &event )
156 { m_panel->DoCopyToClipboard(); }
157 #endif // wxUSE_CLIPBOARD
158
159 void OnAddTextFreeze( wxCommandEvent& event )
160 { DoAddText(true); }
161 void OnAddText( wxCommandEvent& event )
162 { DoAddText(false); }
163 void OnRemoveText( wxCommandEvent& event )
164 { m_panel->DoRemoveText(); }
165
166 void OnMoveToEndOfText( wxCommandEvent &event )
167 { m_panel->DoMoveToEndOfText(); }
168 void OnMoveToEndOfEntry( wxCommandEvent &event )
169 { m_panel->DoMoveToEndOfEntry(); }
170
171 void OnScrollLineDown(wxCommandEvent& event)
172 {
173 if ( !m_panel->m_textrich->LineDown() )
174 wxLogMessage(_T("Already at the bottom"));
175 }
176
177 void OnScrollLineUp(wxCommandEvent& event)
178 {
179 if ( !m_panel->m_textrich->LineUp() )
180 wxLogMessage(_T("Already at the top"));
181 }
182
183 void OnScrollPageDown(wxCommandEvent& event)
184 {
185 if ( !m_panel->m_textrich->PageDown() )
186 wxLogMessage(_T("Already at the bottom"));
187 }
188
189 void OnScrollPageUp(wxCommandEvent& event)
190 {
191 if ( !m_panel->m_textrich->PageUp() )
192 wxLogMessage(_T("Already at the top"));
193 }
194
195 void OnLogClear(wxCommandEvent& event);
196 void OnFileSave(wxCommandEvent& event);
197 void OnFileLoad(wxCommandEvent& event);
198
199 void OnSetEditable(wxCommandEvent& event);
200 void OnSetEnabled(wxCommandEvent& event);
201
202 void OnLogKey(wxCommandEvent& event)
203 {
204 MyTextCtrl::ms_logKey = event.IsChecked();
205 }
206
207 void OnLogChar(wxCommandEvent& event)
208 {
209 MyTextCtrl::ms_logChar = event.IsChecked();
210 }
211
212 void OnLogMouse(wxCommandEvent& event)
213 {
214 MyTextCtrl::ms_logMouse = event.IsChecked();
215 }
216
217 void OnLogText(wxCommandEvent& event)
218 {
219 MyTextCtrl::ms_logText = event.IsChecked();
220 }
221
222 void OnLogFocus(wxCommandEvent& event)
223 {
224 MyTextCtrl::ms_logFocus = event.IsChecked();
225 }
226
227 void OnSetText(wxCommandEvent& event)
228 {
229 m_panel->m_text->SetValue(_T("Hello, world (what else did you expect)?"));
230 }
231
232 void OnIdle( wxIdleEvent& event );
233
234 private:
235 void DoAddText(bool freeze)
236 {
237 wxTextCtrl *text = m_panel->m_textrich;
238 if ( freeze )
239 text->Freeze();
240
241 text->Clear();
242
243 wxProgressDialog dlg(_T("Wait..."), _T("Updating"), 100, this);
244 for ( int i = 0; i < 100; i++ )
245 {
246 dlg.Update(i);
247 text->AppendText(wxString::Format(wxT("Line %i\n"), i));
248 }
249
250 text->SetInsertionPoint(0);
251
252 if ( freeze )
253 text->Thaw();
254 }
255
256 MyPanel *m_panel;
257
258 DECLARE_EVENT_TABLE()
259 };
260
261 //----------------------------------------------------------------------
262 // main()
263 //----------------------------------------------------------------------
264
265 IMPLEMENT_APP(MyApp)
266
267 //----------------------------------------------------------------------
268 // MyApp
269 //----------------------------------------------------------------------
270
271 enum
272 {
273 TEXT_QUIT = 100,
274 TEXT_ABOUT,
275 TEXT_LOAD,
276 TEXT_SAVE,
277 TEXT_CLEAR,
278
279 // clipboard menu
280 TEXT_CLIPBOARD_COPY = 200,
281 TEXT_CLIPBOARD_PASTE,
282
283 // tooltip menu
284 TEXT_TOOLTIPS_SETDELAY = 300,
285 TEXT_TOOLTIPS_ENABLE,
286
287 // text menu
288 TEXT_ADD_SOME = 400,
289 TEXT_ADD_FREEZE,
290 TEXT_MOVE_ENDTEXT,
291 TEXT_MOVE_ENDENTRY,
292 TEXT_SET_EDITABLE,
293 TEXT_SET_ENABLED,
294 TEXT_LINE_DOWN,
295 TEXT_LINE_UP,
296 TEXT_PAGE_DOWN,
297 TEXT_PAGE_UP,
298 TEXT_REMOVE,
299 TEXT_SET,
300
301 // log menu
302 TEXT_LOG_KEY,
303 TEXT_LOG_CHAR,
304 TEXT_LOG_MOUSE,
305 TEXT_LOG_TEXT,
306 TEXT_LOG_FOCUS,
307
308 TEXT_END
309 };
310
311 bool MyApp::OnInit()
312 {
313 // Create the main frame window
314 MyFrame *frame = new MyFrame((wxFrame *) NULL,
315 "Text wxWindows sample", 50, 50, 700, 420);
316 frame->SetSizeHints( 500, 400 );
317
318 wxMenu *file_menu = new wxMenu;
319 file_menu->Append(TEXT_SAVE, "&Save file\tCtrl-S",
320 "Save the text control contents to file");
321 file_menu->Append(TEXT_LOAD, "&Load file\tCtrl-O",
322 "Load the sample file into text control");
323 file_menu->AppendSeparator();
324 file_menu->Append(TEXT_ABOUT, "&About\tAlt-A");
325 file_menu->AppendSeparator();
326 file_menu->Append(TEXT_QUIT, "E&xit\tAlt-X", "Quit this sample");
327
328 wxMenuBar *menu_bar = new wxMenuBar( wxMB_DOCKABLE );
329 menu_bar->Append(file_menu, "&File");
330
331 #if wxUSE_TOOLTIPS
332 wxMenu *tooltip_menu = new wxMenu;
333 tooltip_menu->Append(TEXT_TOOLTIPS_SETDELAY, "Set &delay\tCtrl-D");
334 tooltip_menu->AppendSeparator();
335 tooltip_menu->Append(TEXT_TOOLTIPS_ENABLE, "&Toggle tooltips\tCtrl-T",
336 "enable/disable tooltips", TRUE);
337 tooltip_menu->Check(TEXT_TOOLTIPS_ENABLE, TRUE);
338 menu_bar->Append(tooltip_menu, "&Tooltips");
339 #endif // wxUSE_TOOLTIPS
340
341 #if wxUSE_CLIPBOARD
342 wxMenu *menuClipboard = new wxMenu;
343 menuClipboard->Append(TEXT_CLIPBOARD_COPY, "&Copy\tCtrl-C",
344 "Copy the first line to the clipboard");
345 menuClipboard->Append(TEXT_CLIPBOARD_PASTE, "&Paste\tCtrl-V",
346 "Paste from clipboard to the text control");
347 menu_bar->Append(menuClipboard, "&Clipboard");
348 #endif // wxUSE_CLIPBOARD
349
350 wxMenu *menuText = new wxMenu;
351 menuText->Append(TEXT_ADD_SOME, "&Append some text\tCtrl-A");
352 menuText->Append(TEXT_ADD_FREEZE, "&Append text with freeze/thaw\tShift-Ctrl-A");
353 menuText->Append(TEXT_REMOVE, "&Remove first 10 characters\tCtrl-X");
354 menuText->Append(TEXT_SET, "&Set the first text zone value\tCtrl-E");
355 menuText->AppendSeparator();
356 menuText->Append(TEXT_MOVE_ENDTEXT, "Move cursor to the end of &text");
357 menuText->Append(TEXT_MOVE_ENDENTRY, "Move cursor to the end of &entry");
358 menuText->Append(TEXT_SET_EDITABLE, "Toggle &editable state", "", TRUE);
359 menuText->Append(TEXT_SET_ENABLED, "Toggle e&nabled state", "", TRUE);
360 menuText->Check(TEXT_SET_EDITABLE, TRUE);
361 menuText->Check(TEXT_SET_ENABLED, TRUE);
362 menuText->AppendSeparator();
363 menuText->Append(TEXT_LINE_DOWN, "Scroll text one line down");
364 menuText->Append(TEXT_LINE_UP, "Scroll text one line up");
365 menuText->Append(TEXT_PAGE_DOWN, "Scroll text one page down");
366 menuText->Append(TEXT_PAGE_DOWN, "Scroll text one page up");
367 menu_bar->Append(menuText, "Te&xt");
368
369 wxMenu *menuLog = new wxMenu;
370 menuLog->Append(TEXT_LOG_KEY, "Log &key events", "", TRUE);
371 menuLog->Append(TEXT_LOG_CHAR, "Log &char events", "", TRUE);
372 menuLog->Append(TEXT_LOG_MOUSE, "Log &mouse events", "", TRUE);
373 menuLog->Append(TEXT_LOG_TEXT, "Log &text events", "", TRUE);
374 menuLog->Append(TEXT_LOG_FOCUS, "Log &focus events", "", TRUE);
375 menuLog->AppendSeparator();
376 menuLog->Append(TEXT_CLEAR, "&Clear the log\tCtrl-C",
377 "Clear the log window contents");
378
379 // select only the interesting events by default
380 #if 0
381 menuLog->Check(TEXT_LOG_KEY, TRUE);
382 menuLog->Check(TEXT_LOG_CHAR, TRUE);
383 menuLog->Check(TEXT_LOG_TEXT, TRUE);
384
385 MyTextCtrl::ms_logKey =
386 MyTextCtrl::ms_logChar =
387 MyTextCtrl::ms_logText = TRUE;
388 #else
389 menuLog->Check(TEXT_LOG_FOCUS, TRUE);
390 MyTextCtrl::ms_logFocus = TRUE;
391 #endif
392 menu_bar->Append(menuLog, "&Log");
393
394 frame->SetMenuBar(menu_bar);
395
396 frame->Show(TRUE);
397
398 SetTopWindow(frame);
399
400 // report success
401 return TRUE;
402 }
403
404 //----------------------------------------------------------------------
405 // MyTextCtrl
406 //----------------------------------------------------------------------
407
408 BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
409 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
410 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
411 EVT_CHAR(MyTextCtrl::OnChar)
412
413 EVT_TEXT(-1, MyTextCtrl::OnText)
414 EVT_TEXT_URL(-1, MyTextCtrl::OnTextURL)
415 EVT_TEXT_MAXLEN(-1, MyTextCtrl::OnTextMaxLen)
416
417 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent)
418
419 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus)
420 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus)
421 END_EVENT_TABLE()
422
423 bool MyTextCtrl::ms_logKey = FALSE;
424 bool MyTextCtrl::ms_logChar = FALSE;
425 bool MyTextCtrl::ms_logMouse = FALSE;
426 bool MyTextCtrl::ms_logText = FALSE;
427 bool MyTextCtrl::ms_logFocus = FALSE;
428
429 void MyTextCtrl::LogKeyEvent(const wxChar *name, wxKeyEvent& event) const
430 {
431 wxString key;
432 long keycode = event.KeyCode();
433 {
434 switch ( keycode )
435 {
436 case WXK_BACK: key = "BACK"; break;
437 case WXK_TAB: key = "TAB"; break;
438 case WXK_RETURN: key = "RETURN"; break;
439 case WXK_ESCAPE: key = "ESCAPE"; break;
440 case WXK_SPACE: key = "SPACE"; break;
441 case WXK_DELETE: key = "DELETE"; break;
442 case WXK_START: key = "START"; break;
443 case WXK_LBUTTON: key = "LBUTTON"; break;
444 case WXK_RBUTTON: key = "RBUTTON"; break;
445 case WXK_CANCEL: key = "CANCEL"; break;
446 case WXK_MBUTTON: key = "MBUTTON"; break;
447 case WXK_CLEAR: key = "CLEAR"; break;
448 case WXK_SHIFT: key = "SHIFT"; break;
449 case WXK_ALT: key = "ALT"; break;
450 case WXK_CONTROL: key = "CONTROL"; break;
451 case WXK_MENU: key = "MENU"; break;
452 case WXK_PAUSE: key = "PAUSE"; break;
453 case WXK_CAPITAL: key = "CAPITAL"; break;
454 case WXK_PRIOR: key = "PRIOR"; break;
455 case WXK_NEXT: key = "NEXT"; break;
456 case WXK_END: key = "END"; break;
457 case WXK_HOME: key = "HOME"; break;
458 case WXK_LEFT: key = "LEFT"; break;
459 case WXK_UP: key = "UP"; break;
460 case WXK_RIGHT: key = "RIGHT"; break;
461 case WXK_DOWN: key = "DOWN"; break;
462 case WXK_SELECT: key = "SELECT"; break;
463 case WXK_PRINT: key = "PRINT"; break;
464 case WXK_EXECUTE: key = "EXECUTE"; break;
465 case WXK_SNAPSHOT: key = "SNAPSHOT"; break;
466 case WXK_INSERT: key = "INSERT"; break;
467 case WXK_HELP: key = "HELP"; break;
468 case WXK_NUMPAD0: key = "NUMPAD0"; break;
469 case WXK_NUMPAD1: key = "NUMPAD1"; break;
470 case WXK_NUMPAD2: key = "NUMPAD2"; break;
471 case WXK_NUMPAD3: key = "NUMPAD3"; break;
472 case WXK_NUMPAD4: key = "NUMPAD4"; break;
473 case WXK_NUMPAD5: key = "NUMPAD5"; break;
474 case WXK_NUMPAD6: key = "NUMPAD6"; break;
475 case WXK_NUMPAD7: key = "NUMPAD7"; break;
476 case WXK_NUMPAD8: key = "NUMPAD8"; break;
477 case WXK_NUMPAD9: key = "NUMPAD9"; break;
478 case WXK_MULTIPLY: key = "MULTIPLY"; break;
479 case WXK_ADD: key = "ADD"; break;
480 case WXK_SEPARATOR: key = "SEPARATOR"; break;
481 case WXK_SUBTRACT: key = "SUBTRACT"; break;
482 case WXK_DECIMAL: key = "DECIMAL"; break;
483 case WXK_DIVIDE: key = "DIVIDE"; break;
484 case WXK_F1: key = "F1"; break;
485 case WXK_F2: key = "F2"; break;
486 case WXK_F3: key = "F3"; break;
487 case WXK_F4: key = "F4"; break;
488 case WXK_F5: key = "F5"; break;
489 case WXK_F6: key = "F6"; break;
490 case WXK_F7: key = "F7"; break;
491 case WXK_F8: key = "F8"; break;
492 case WXK_F9: key = "F9"; break;
493 case WXK_F10: key = "F10"; break;
494 case WXK_F11: key = "F11"; break;
495 case WXK_F12: key = "F12"; break;
496 case WXK_F13: key = "F13"; break;
497 case WXK_F14: key = "F14"; break;
498 case WXK_F15: key = "F15"; break;
499 case WXK_F16: key = "F16"; break;
500 case WXK_F17: key = "F17"; break;
501 case WXK_F18: key = "F18"; break;
502 case WXK_F19: key = "F19"; break;
503 case WXK_F20: key = "F20"; break;
504 case WXK_F21: key = "F21"; break;
505 case WXK_F22: key = "F22"; break;
506 case WXK_F23: key = "F23"; break;
507 case WXK_F24: key = "F24"; break;
508 case WXK_NUMLOCK: key = "NUMLOCK"; break;
509 case WXK_SCROLL: key = "SCROLL"; break;
510 case WXK_PAGEUP: key = "PAGEUP"; break;
511 case WXK_PAGEDOWN: key = "PAGEDOWN"; break;
512 case WXK_NUMPAD_SPACE: key = "NUMPAD_SPACE"; break;
513 case WXK_NUMPAD_TAB: key = "NUMPAD_TAB"; break;
514 case WXK_NUMPAD_ENTER: key = "NUMPAD_ENTER"; break;
515 case WXK_NUMPAD_F1: key = "NUMPAD_F1"; break;
516 case WXK_NUMPAD_F2: key = "NUMPAD_F2"; break;
517 case WXK_NUMPAD_F3: key = "NUMPAD_F3"; break;
518 case WXK_NUMPAD_F4: key = "NUMPAD_F4"; break;
519 case WXK_NUMPAD_HOME: key = "NUMPAD_HOME"; break;
520 case WXK_NUMPAD_LEFT: key = "NUMPAD_LEFT"; break;
521 case WXK_NUMPAD_UP: key = "NUMPAD_UP"; break;
522 case WXK_NUMPAD_RIGHT: key = "NUMPAD_RIGHT"; break;
523 case WXK_NUMPAD_DOWN: key = "NUMPAD_DOWN"; break;
524 case WXK_NUMPAD_PRIOR: key = "NUMPAD_PRIOR"; break;
525 case WXK_NUMPAD_PAGEUP: key = "NUMPAD_PAGEUP"; break;
526 case WXK_NUMPAD_PAGEDOWN: key = "NUMPAD_PAGEDOWN"; break;
527 case WXK_NUMPAD_END: key = "NUMPAD_END"; break;
528 case WXK_NUMPAD_BEGIN: key = "NUMPAD_BEGIN"; break;
529 case WXK_NUMPAD_INSERT: key = "NUMPAD_INSERT"; break;
530 case WXK_NUMPAD_DELETE: key = "NUMPAD_DELETE"; break;
531 case WXK_NUMPAD_EQUAL: key = "NUMPAD_EQUAL"; break;
532 case WXK_NUMPAD_MULTIPLY: key = "NUMPAD_MULTIPLY"; break;
533 case WXK_NUMPAD_ADD: key = "NUMPAD_ADD"; break;
534 case WXK_NUMPAD_SEPARATOR: key = "NUMPAD_SEPARATOR"; break;
535 case WXK_NUMPAD_SUBTRACT: key = "NUMPAD_SUBTRACT"; break;
536 case WXK_NUMPAD_DECIMAL: key = "NUMPAD_DECIMAL"; break;
537
538 default:
539 {
540 if ( wxIsprint((int)keycode) )
541 key.Printf(_T("'%c'"), (char)keycode);
542 else if ( keycode > 0 && keycode < 27 )
543 key.Printf(_("Ctrl-%c"), _T('A') + keycode - 1);
544 else
545 key.Printf(_T("unknown (%ld)"), keycode);
546 }
547 }
548 }
549
550 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
551 name,
552 key.c_str(),
553 GetChar( event.ControlDown(), _T('C') ),
554 GetChar( event.AltDown(), _T('A') ),
555 GetChar( event.ShiftDown(), _T('S') ),
556 GetChar( event.MetaDown(), _T('M') ) );
557 }
558
559 static wxString GetMouseEventDesc(const wxMouseEvent& ev)
560 {
561 // click event
562 wxString button;
563 bool dbl, up;
564 if ( ev.LeftDown() || ev.LeftUp() || ev.LeftDClick() )
565 {
566 button = _T("Left");
567 dbl = ev.LeftDClick();
568 up = ev.LeftUp();
569 }
570 else if ( ev.MiddleDown() || ev.MiddleUp() || ev.MiddleDClick() )
571 {
572 button = _T("Middle");
573 dbl = ev.MiddleDClick();
574 up = ev.MiddleUp();
575 }
576 else if ( ev.RightDown() || ev.RightUp() || ev.RightDClick() )
577 {
578 button = _T("Right");
579 dbl = ev.RightDClick();
580 up = ev.RightUp();
581 }
582 else
583 {
584 return _T("Unknown mouse event");
585 }
586
587 return wxString::Format(_T("%s mouse button %s"),
588 button.c_str(),
589 dbl ? _T("double clicked")
590 : up ? _T("released") : _T("clicked"));
591 }
592
593 void MyTextCtrl::OnMouseEvent(wxMouseEvent& ev)
594 {
595 ev.Skip();
596
597 if ( !ms_logMouse )
598 return;
599
600 if ( !ev.Moving() )
601 {
602 wxString msg;
603 if ( ev.Entering() )
604 {
605 msg = _T("Mouse entered the window");
606 }
607 else if ( ev.Leaving() )
608 {
609 msg = _T("Mouse left the window");
610 }
611 else
612 {
613 msg = GetMouseEventDesc(ev);
614 }
615
616 msg << _T(" at (") << ev.GetX() << _T(", ") << ev.GetY() << _T(") ")
617 << _T("Flags: ")
618 << GetChar( ev.LeftIsDown(), _T('1') )
619 << GetChar( ev.MiddleIsDown(), _T('2') )
620 << GetChar( ev.RightIsDown(), _T('3') )
621 << GetChar( ev.ControlDown(), _T('C') )
622 << GetChar( ev.AltDown(), _T('A') )
623 << GetChar( ev.ShiftDown(), _T('S') )
624 << GetChar( ev.MetaDown(), _T('M') );
625
626 wxLogMessage(msg);
627 }
628 //else: we're not interested in mouse move events
629 }
630
631 void MyTextCtrl::OnSetFocus(wxFocusEvent& event)
632 {
633 if ( ms_logFocus )
634 wxLogMessage("%p got focus.", this);
635
636 event.Skip();
637 }
638
639 void MyTextCtrl::OnKillFocus(wxFocusEvent& event)
640 {
641 if ( ms_logFocus )
642 wxLogMessage("%p lost focus", this);
643
644 event.Skip();
645 }
646
647 void MyTextCtrl::OnText(wxCommandEvent& event)
648 {
649 if ( !ms_logText )
650 return;
651
652 MyTextCtrl *win = (MyTextCtrl *)event.GetEventObject();
653 const wxChar *data = (const wxChar *)(win->GetClientData());
654 if ( data )
655 {
656 wxLogMessage(_T("Text changed in control '%s'"), data);
657 }
658 else
659 {
660 wxLogMessage(_T("Text changed in some control"));
661 }
662 }
663
664 void MyTextCtrl::OnTextMaxLen(wxCommandEvent& event)
665 {
666 wxLogMessage(_T("You can't enter more characters into this control."));
667 }
668
669 void MyTextCtrl::OnTextURL(wxTextUrlEvent& event)
670 {
671 const wxMouseEvent& ev = event.GetMouseEvent();
672
673 // filter out mouse moves, too many of them
674 if ( ev.Moving() )
675 return;
676
677 long start = event.GetURLStart(),
678 end = event.GetURLEnd();
679
680 wxLogMessage(_T("Mouse event over URL '%s': %s"),
681 GetValue().Mid(start, end - start).c_str(),
682 GetMouseEventDesc(ev).c_str());
683 }
684
685 void MyTextCtrl::OnChar(wxKeyEvent& event)
686 {
687 if ( ms_logChar )
688 LogKeyEvent( _T("Char"), event);
689
690 event.Skip();
691 }
692
693 void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
694 {
695 if ( ms_logKey )
696 LogKeyEvent( _T("Key up"), event);
697
698 event.Skip();
699 }
700
701 void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
702 {
703 switch ( event.KeyCode() )
704 {
705 case WXK_F1:
706 // show current position and text length
707 {
708 long line, column, pos = GetInsertionPoint();
709 PositionToXY(pos, &column, &line);
710
711 wxLogMessage( _T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
712 pos,
713 line, column,
714 GetNumberOfLines(),
715 GetLineLength(line),
716 GetValue().length(),
717 GetLastPosition());
718 }
719 break;
720
721 case WXK_F2:
722 // go to the end
723 SetInsertionPointEnd();
724 break;
725
726 case WXK_F3:
727 // go to position 10
728 SetInsertionPoint(10);
729 break;
730
731 case WXK_F4:
732 if (!m_hasCapture)
733 {
734 wxLogDebug( wxT("Now capturing mouse and events.") );
735 m_hasCapture = TRUE;
736 CaptureMouse();
737 }
738 else
739 {
740 wxLogDebug( wxT("Stopped capturing mouse and events.") );
741 m_hasCapture = FALSE;
742 ReleaseMouse();
743 }
744 break;
745
746 case WXK_F5:
747 // insert a blank line
748 WriteText("\n");
749 break;
750
751 case WXK_F6:
752 SetValue("F6 was just pressed.");
753 break;
754
755 case WXK_F7:
756 ShowPosition(10);
757 break;
758
759 case WXK_F10:
760 {
761 long from, to;
762 GetSelection(&from, &to);
763
764 wxString sel = GetStringSelection();
765
766 wxLogMessage(_T("Selection: from %ld to %ld."), from, to);
767 wxLogMessage(_T("Selection = '%s' (len = %u)"),
768 sel.c_str(), sel.length());
769 }
770 }
771
772 if ( ms_logKey )
773 LogKeyEvent( wxT("Key down"), event);
774
775 event.Skip();
776 }
777
778 //----------------------------------------------------------------------
779 // MyPanel
780 //----------------------------------------------------------------------
781
782 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
783 EVT_SIZE(MyPanel::OnSize)
784 END_EVENT_TABLE()
785
786 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
787 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
788 {
789 m_log = new wxTextCtrl( this, -1, "This is the log window.\n",
790 wxPoint(5,260), wxSize(630,100),
791 wxTE_MULTILINE | wxTE_READONLY /* | wxTE_RICH */);
792
793 m_logOld = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
794
795 // single line text controls
796
797 m_text = new MyTextCtrl( this, -1, "Single line.",
798 wxPoint(10,10), wxSize(140,-1),
799 wxTE_PROCESS_ENTER);
800 m_text->SetForegroundColour(*wxBLUE);
801 m_text->SetBackgroundColour(*wxLIGHT_GREY);
802 (*m_text) << " Appended.";
803 m_text->SetInsertionPoint(0);
804 m_text->WriteText( "Prepended. " );
805
806 m_password = new MyTextCtrl( this, -1, "",
807 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD );
808
809 m_readonly = new MyTextCtrl( this, -1, "Read only",
810 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY );
811
812 m_limited = new MyTextCtrl(this, -1, "Max 8 ch",
813 wxPoint(10, 130), wxSize(140, -1));
814 m_limited->SetMaxLength(8);
815
816 // multi line text controls
817
818 m_horizontal = new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
819 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL );
820
821 // a little hack to use the command line argument for encoding testing
822 if ( wxTheApp->argc == 2 )
823 {
824 switch ( wxTheApp->argv[1][0] )
825 {
826 case '2':
827 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
828 FALSE, "",
829 wxFONTENCODING_ISO8859_2));
830 m_horizontal->SetValue("®lu»ouèký kùò zbìsile èe¹tina «»");
831 break;
832
833 case '1':
834 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
835 FALSE, "",
836 wxFONTENCODING_CP1251));
837 m_horizontal->SetValue("Ïðèâåò!");
838 break;
839
840 case '8':
841 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
842 FALSE, "",
843 wxFONTENCODING_CP1251));
844 #if wxUSE_UNICODE
845 m_horizontal->SetValue(L"\x0412\x0430\x0434\x0438\x043c \x0426");
846 #else
847 m_horizontal->SetValue("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
848 #endif
849 }
850 }
851 else
852 {
853 m_horizontal->SetValue("Text in default encoding");
854 }
855
856 m_multitext = new MyTextCtrl( this, -1, "Multi line.",
857 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE );
858 m_multitext->SetFont(*wxITALIC_FONT);
859 (*m_multitext) << " Appended.";
860 m_multitext->SetInsertionPoint(0);
861 m_multitext->WriteText( "Prepended. " );
862 m_multitext->SetForegroundColour(*wxRED);
863 m_multitext->SetBackgroundColour(*wxLIGHT_GREY);
864
865 #if wxUSE_TOOLTIPS
866 m_multitext->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
867 #endif
868
869 m_tab = new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
870 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE | wxTE_PROCESS_TAB );
871 m_tab->SetClientData((void *)_T("tab"));
872
873 m_enter = new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
874 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE);
875 m_enter->SetClientData((void *)_T("enter"));
876
877 m_textrich = new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
878 "(even under broken Win9x)\n"
879 "and a very very very very very "
880 "very very very long line to test "
881 "wxHSCROLL style",
882 wxPoint(450, 10), wxSize(230, 230),
883 wxTE_RICH |
884 wxTE_MULTILINE |
885 // wxTE_AUTO_URL |
886 wxHSCROLL);
887
888 m_textrich->SetStyle(0, 10, *wxRED);
889 m_textrich->SetStyle(10, 20, *wxBLUE);
890 m_textrich->SetStyle(30, 40,
891 wxTextAttr(*wxGREEN, wxNullColour, *wxITALIC_FONT));
892 m_textrich->SetDefaultStyle(wxTextAttr());
893 m_textrich->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
894 m_textrich->AppendText(_T("Next 10 characters should be in blue\n"));
895 m_textrich->AppendText(_T("Next 10 characters should be normal\n"));
896 m_textrich->AppendText(_T("And the next 10 characters should be green and italic\n"));
897 m_textrich->SetDefaultStyle(wxTextAttr(*wxCYAN, *wxBLUE));
898 m_textrich->AppendText(_T("This text should be cyan on blue\n"));
899 m_textrich->SetDefaultStyle(wxTextAttr(*wxBLUE, *wxWHITE));
900 m_textrich->AppendText(_T("And this should be in blue and the text you ")
901 _T("type should be in blue as well"));
902 }
903
904 void MyPanel::OnSize( wxSizeEvent &event )
905 {
906 wxSize client_area( GetClientSize() );
907 m_log->SetSize( 0, 260, client_area.x, client_area.y - 260 );
908 event.Skip();
909 }
910
911 wxTextCtrl *MyPanel::GetFocusedText(wxTextCtrl *textDef)
912 {
913 wxWindow *win = FindFocus();
914
915 wxTextCtrl *text = win ? wxDynamicCast(win, wxTextCtrl) : NULL;
916 return text ? text : textDef;
917 }
918
919 #if wxUSE_CLIPBOARD
920 void MyPanel::DoPasteFromClipboard()
921 {
922 // On X11, we want to get the data from the primary selection instead
923 // of the normal clipboard (which isn't normal under X11 at all). This
924 // call has no effect under MSW.
925 wxTheClipboard->UsePrimarySelection();
926
927 if (!wxTheClipboard->Open())
928 {
929 *m_log << "Error opening the clipboard.\n";
930 return;
931 }
932 else
933 {
934 *m_log << "Successfully opened the clipboard.\n";
935 }
936
937 wxTextDataObject data;
938
939 if (wxTheClipboard->IsSupported( data.GetFormat() ))
940 {
941 *m_log << "Clipboard supports requested format.\n";
942
943 if (wxTheClipboard->GetData( data ))
944 {
945 *m_log << "Successfully retrieved data from the clipboard.\n";
946 *m_multitext << data.GetText() << "\n";
947 }
948 else
949 {
950 *m_log << "Error getting data from the clipboard.\n";
951 }
952 }
953 else
954 {
955 *m_log << "Clipboard doesn't support requested format.\n";
956 }
957
958 wxTheClipboard->Close();
959
960 *m_log << "Closed the clipboard.\n";
961 }
962
963 void MyPanel::DoCopyToClipboard()
964 {
965 // On X11, we want to get the data from the primary selection instead
966 // of the normal clipboard (which isn't normal under X11 at all). This
967 // call has no effect under MSW.
968 wxTheClipboard->UsePrimarySelection();
969
970 wxString text( m_multitext->GetLineText(0) );
971
972 if (text.IsEmpty())
973 {
974 *m_log << "No text to copy.\n";
975
976 return;
977 }
978
979 if (!wxTheClipboard->Open())
980 {
981 *m_log << "Error opening the clipboard.\n";
982
983 return;
984 }
985 else
986 {
987 *m_log << "Successfully opened the clipboard.\n";
988 }
989
990 wxTextDataObject *data = new wxTextDataObject( text );
991
992 if (!wxTheClipboard->SetData( data ))
993 {
994 *m_log << "Error while copying to the clipboard.\n";
995 }
996 else
997 {
998 *m_log << "Successfully copied data to the clipboard.\n";
999 }
1000
1001 wxTheClipboard->Close();
1002
1003 *m_log << "Closed the clipboard.\n";
1004 }
1005
1006 #endif // wxUSE_CLIPBOARD
1007
1008 void MyPanel::DoMoveToEndOfText()
1009 {
1010 m_multitext->SetInsertionPointEnd();
1011 m_multitext->SetFocus();
1012 }
1013
1014 void MyPanel::DoMoveToEndOfEntry()
1015 {
1016 m_text->SetInsertionPointEnd();
1017 m_text->SetFocus();
1018 }
1019
1020 void MyPanel::DoRemoveText()
1021 {
1022 GetFocusedText(m_multitext)->Remove(0, 10);
1023 }
1024
1025 //----------------------------------------------------------------------
1026 // MyFrame
1027 //----------------------------------------------------------------------
1028
1029 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
1030 EVT_MENU(TEXT_QUIT, MyFrame::OnQuit)
1031 EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout)
1032 EVT_MENU(TEXT_SAVE, MyFrame::OnFileSave)
1033 EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad)
1034
1035 EVT_MENU(TEXT_LOG_KEY, MyFrame::OnLogKey)
1036 EVT_MENU(TEXT_LOG_CHAR, MyFrame::OnLogChar)
1037 EVT_MENU(TEXT_LOG_MOUSE,MyFrame::OnLogMouse)
1038 EVT_MENU(TEXT_LOG_TEXT, MyFrame::OnLogText)
1039 EVT_MENU(TEXT_LOG_FOCUS,MyFrame::OnLogFocus)
1040 EVT_MENU(TEXT_CLEAR, MyFrame::OnLogClear)
1041
1042 #if wxUSE_TOOLTIPS
1043 EVT_MENU(TEXT_TOOLTIPS_SETDELAY, MyFrame::OnSetTooltipDelay)
1044 EVT_MENU(TEXT_TOOLTIPS_ENABLE, MyFrame::OnToggleTooltips)
1045 #endif // wxUSE_TOOLTIPS
1046
1047 #if wxUSE_CLIPBOARD
1048 EVT_MENU(TEXT_CLIPBOARD_PASTE, MyFrame::OnPasteFromClipboard)
1049 EVT_MENU(TEXT_CLIPBOARD_COPY, MyFrame::OnCopyToClipboard)
1050 #endif // wxUSE_CLIPBOARD
1051
1052 EVT_MENU(TEXT_REMOVE, MyFrame::OnRemoveText)
1053 EVT_MENU(TEXT_ADD_SOME, MyFrame::OnAddText)
1054 EVT_MENU(TEXT_ADD_FREEZE, MyFrame::OnAddTextFreeze)
1055 EVT_MENU(TEXT_MOVE_ENDTEXT, MyFrame::OnMoveToEndOfText)
1056 EVT_MENU(TEXT_MOVE_ENDENTRY, MyFrame::OnMoveToEndOfEntry)
1057
1058 EVT_MENU(TEXT_SET_EDITABLE, MyFrame::OnSetEditable)
1059 EVT_MENU(TEXT_SET_ENABLED, MyFrame::OnSetEnabled)
1060
1061 EVT_MENU(TEXT_LINE_DOWN, MyFrame::OnScrollLineDown)
1062 EVT_MENU(TEXT_LINE_UP, MyFrame::OnScrollLineUp)
1063 EVT_MENU(TEXT_PAGE_DOWN, MyFrame::OnScrollPageDown)
1064 EVT_MENU(TEXT_PAGE_UP, MyFrame::OnScrollPageUp)
1065
1066 EVT_MENU(TEXT_SET, MyFrame::OnSetText)
1067
1068 EVT_IDLE(MyFrame::OnIdle)
1069 END_EVENT_TABLE()
1070
1071 MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h)
1072 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h) )
1073 {
1074 CreateStatusBar(2);
1075
1076 m_panel = new MyPanel( this, 10, 10, 300, 100 );
1077 }
1078
1079 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
1080 {
1081 Close(TRUE);
1082 }
1083
1084 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
1085 {
1086 wxBeginBusyCursor();
1087
1088 wxMessageDialog dialog(this,
1089 "This is a text control sample. It demonstrates the many different\n"
1090 "text control styles, the use of the clipboard, setting and handling\n"
1091 "tooltips and intercepting key and char events.\n"
1092 "\n"
1093 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
1094 "About wxTextCtrl Sample",
1095 wxOK | wxICON_INFORMATION);
1096
1097 dialog.ShowModal();
1098
1099 wxEndBusyCursor();
1100 }
1101
1102 #if wxUSE_TOOLTIPS
1103 void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
1104 {
1105 static long s_delay = 5000;
1106
1107 wxString delay;
1108 delay.Printf( _T("%ld"), s_delay);
1109
1110 delay = wxGetTextFromUser("Enter delay (in milliseconds)",
1111 "Set tooltip delay",
1112 delay,
1113 this);
1114 if ( !delay )
1115 return; // cancelled
1116
1117 wxSscanf(delay, _T("%ld"), &s_delay);
1118
1119 wxToolTip::SetDelay(s_delay);
1120
1121 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
1122 }
1123
1124 void MyFrame::OnToggleTooltips(wxCommandEvent& event)
1125 {
1126 static bool s_enabled = TRUE;
1127
1128 s_enabled = !s_enabled;
1129
1130 wxToolTip::Enable(s_enabled);
1131
1132 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
1133 }
1134 #endif // tooltips
1135
1136 void MyFrame::OnLogClear(wxCommandEvent& WXUNUSED(event))
1137 {
1138 m_panel->m_log->Clear();
1139 }
1140
1141 void MyFrame::OnSetEditable(wxCommandEvent& WXUNUSED(event))
1142 {
1143 static bool s_editable = TRUE;
1144
1145 s_editable = !s_editable;
1146 m_panel->m_text->SetEditable(s_editable);
1147 m_panel->m_password->SetEditable(s_editable);
1148 m_panel->m_multitext->SetEditable(s_editable);
1149 m_panel->m_textrich->SetEditable(s_editable);
1150 }
1151
1152 void MyFrame::OnSetEnabled(wxCommandEvent& WXUNUSED(event))
1153 {
1154 bool enabled = m_panel->m_text->IsEnabled();
1155 enabled = !enabled;
1156
1157 m_panel->m_text->Enable(enabled);
1158 m_panel->m_password->Enable(enabled);
1159 m_panel->m_multitext->Enable(enabled);
1160 m_panel->m_readonly->Enable(enabled);
1161 m_panel->m_limited->Enable(enabled);
1162 m_panel->m_textrich->Enable(enabled);
1163 }
1164
1165 void MyFrame::OnFileSave(wxCommandEvent& event)
1166 {
1167 if ( m_panel->m_textrich->SaveFile("dummy.txt") )
1168 {
1169 #if wxUSE_FILE
1170 // verify that the fil length is correct (it wasn't under Win95)
1171 wxFile file(wxT("dummy.txt"));
1172 wxLogStatus(this, _T("Successfully saved file (text len = %ld, file size = %ld)"),
1173 m_panel->m_textrich->GetValue().length(),
1174 file.Length());
1175 #endif
1176 }
1177 else
1178 wxLogStatus(this, _T("Couldn't save the file"));
1179 }
1180
1181 void MyFrame::OnFileLoad(wxCommandEvent& event)
1182 {
1183 if ( m_panel->m_textrich->LoadFile("dummy.txt") )
1184 wxLogStatus(this, _T("Successfully loaded file"));
1185 else
1186 wxLogStatus(this, _T("Couldn't load the file"));
1187 }
1188
1189 void MyFrame::OnIdle( wxIdleEvent& event )
1190 {
1191 // track the window which has the focus in the status bar
1192 static wxWindow *s_windowFocus = (wxWindow *)NULL;
1193 wxWindow *focus = wxWindow::FindFocus();
1194 if ( focus && (focus != s_windowFocus) )
1195 {
1196 s_windowFocus = focus;
1197
1198 wxString msg;
1199 msg.Printf(
1200 #ifdef __WXMSW__
1201 _T("Focus: wxWindow = %p, HWND = %08x"),
1202 #else
1203 _T("Focus: wxWindow = %p"),
1204 #endif
1205 s_windowFocus
1206 #ifdef __WXMSW__
1207 , s_windowFocus->GetHWND()
1208 #endif
1209 );
1210
1211 SetStatusText(msg);
1212 }
1213 event.Skip();
1214 }