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