]>
git.saurik.com Git - wxWidgets.git/blob - samples/text/text.cpp
fe6307d5f71552c0113c143a6e213bc2bfdf8178
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: TextCtrl wxWindows sample
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "text.cpp"
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
27 #include "wx/dataobj.h"
28 #include "wx/clipbrd.h"
36 #include "wx/tooltip.h"
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
47 //----------------------------------------------------------------------
49 //----------------------------------------------------------------------
51 class MyApp
: public wxApp
57 // a text ctrl which allows to call different wxTextCtrl functions
58 // interactively by pressing function keys in it
59 class MyTextCtrl
: public wxTextCtrl
62 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
63 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
64 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
)
69 void OnKeyDown(wxKeyEvent
& event
);
70 void OnKeyUp(wxKeyEvent
& event
);
71 void OnChar(wxKeyEvent
& event
);
73 void OnText(wxCommandEvent
& event
);
74 void OnTextURL(wxTextUrlEvent
& event
);
75 void OnTextMaxLen(wxCommandEvent
& event
);
77 void OnMouseEvent(wxMouseEvent
& event
);
79 static bool ms_logKey
;
80 static bool ms_logChar
;
81 static bool ms_logMouse
;
82 static bool ms_logText
;
85 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
86 void LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
93 class MyPanel
: public wxPanel
96 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
99 void DoPasteFromClipboard();
100 void DoCopyToClipboard();
101 #endif // wxUSE_CLIPBOARD
103 void DoMoveToEndOfText();
104 void DoMoveToEndOfEntry();
106 void OnSize( wxSizeEvent
&event
);
109 MyTextCtrl
*m_password
;
112 MyTextCtrl
*m_readonly
;
113 MyTextCtrl
*m_limited
;
115 MyTextCtrl
*m_multitext
;
116 MyTextCtrl
*m_horizontal
;
118 MyTextCtrl
*m_textrich
;
123 DECLARE_EVENT_TABLE()
126 class MyFrame
: public wxFrame
129 MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
);
131 void OnQuit(wxCommandEvent
& event
);
132 void OnAbout(wxCommandEvent
& event
);
134 void OnSetTooltipDelay(wxCommandEvent
& event
);
135 void OnToggleTooltips(wxCommandEvent
& event
);
136 #endif // wxUSE_TOOLTIPS
139 void OnPasteFromClipboard( wxCommandEvent
&event
)
140 { m_panel
->DoPasteFromClipboard(); }
141 void OnCopyToClipboard( wxCommandEvent
&event
)
142 { m_panel
->DoCopyToClipboard(); }
143 #endif // wxUSE_CLIPBOARD
145 void OnAddTextFreeze( wxCommandEvent
& event
)
147 void OnAddText( wxCommandEvent
& event
)
148 { DoAddText(false); }
150 void OnMoveToEndOfText( wxCommandEvent
&event
)
151 { m_panel
->DoMoveToEndOfText(); }
152 void OnMoveToEndOfEntry( wxCommandEvent
&event
)
153 { m_panel
->DoMoveToEndOfEntry(); }
155 void OnScrollLineDown(wxCommandEvent
& event
)
157 if ( !m_panel
->m_textrich
->LineDown() )
158 wxLogMessage(_T("Already at the bottom"));
161 void OnScrollLineUp(wxCommandEvent
& event
)
163 if ( !m_panel
->m_textrich
->LineUp() )
164 wxLogMessage(_T("Already at the top"));
167 void OnScrollPageDown(wxCommandEvent
& event
)
169 if ( !m_panel
->m_textrich
->PageDown() )
170 wxLogMessage(_T("Already at the bottom"));
173 void OnScrollPageUp(wxCommandEvent
& event
)
175 if ( !m_panel
->m_textrich
->PageUp() )
176 wxLogMessage(_T("Already at the top"));
179 void OnLogClear(wxCommandEvent
& event
);
180 void OnFileSave(wxCommandEvent
& event
);
181 void OnFileLoad(wxCommandEvent
& event
);
183 void OnSetEditable(wxCommandEvent
& event
);
184 void OnSetEnabled(wxCommandEvent
& event
);
186 void OnLogKey(wxCommandEvent
& event
)
188 MyTextCtrl::ms_logKey
= event
.IsChecked();
191 void OnLogChar(wxCommandEvent
& event
)
193 MyTextCtrl::ms_logChar
= event
.IsChecked();
196 void OnLogMouse(wxCommandEvent
& event
)
198 MyTextCtrl::ms_logMouse
= event
.IsChecked();
201 void OnLogText(wxCommandEvent
& event
)
203 MyTextCtrl::ms_logText
= event
.IsChecked();
206 void OnIdle( wxIdleEvent
& event
);
209 void DoAddText(bool freeze
)
211 wxTextCtrl
*text
= m_panel
->m_textrich
;
215 for ( int i
= 0; i
< 100; i
++ )
216 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
218 text
->SetInsertionPoint(0);
226 DECLARE_EVENT_TABLE()
229 //----------------------------------------------------------------------
231 //----------------------------------------------------------------------
235 //----------------------------------------------------------------------
237 //----------------------------------------------------------------------
248 TEXT_CLIPBOARD_COPY
= 200,
249 TEXT_CLIPBOARD_PASTE
,
252 TEXT_TOOLTIPS_SETDELAY
= 300,
253 TEXT_TOOLTIPS_ENABLE
,
278 // Create the main frame window
279 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
280 "Text wxWindows sample", 50, 50, 700, 420);
281 frame
->SetSizeHints( 500, 400 );
283 wxMenu
*file_menu
= new wxMenu
;
284 file_menu
->Append(TEXT_SAVE
, "&Save file\tCtrl-S",
285 "Save the text control contents to file");
286 file_menu
->Append(TEXT_LOAD
, "&Load file\tCtrl-O",
287 "Load the sample file into text control");
288 file_menu
->AppendSeparator();
289 file_menu
->Append(TEXT_ABOUT
, "&About\tAlt-A");
290 file_menu
->AppendSeparator();
291 file_menu
->Append(TEXT_QUIT
, "E&xit\tAlt-X", "Quit this sample");
293 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
294 menu_bar
->Append(file_menu
, "&File");
297 wxMenu
*tooltip_menu
= new wxMenu
;
298 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, "Set &delay\tCtrl-D");
299 tooltip_menu
->AppendSeparator();
300 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, "&Toggle tooltips\tCtrl-T",
301 "enable/disable tooltips", TRUE
);
302 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, TRUE
);
303 menu_bar
->Append(tooltip_menu
, "&Tooltips");
304 #endif // wxUSE_TOOLTIPS
307 wxMenu
*menuClipboard
= new wxMenu
;
308 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, "&Copy\tCtrl-C",
309 "Copy the first line to the clipboard");
310 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, "&Paste\tCtrl-V",
311 "Paste from clipboard to the text control");
312 menu_bar
->Append(menuClipboard
, "&Clipboard");
313 #endif // wxUSE_CLIPBOARD
315 wxMenu
*menuText
= new wxMenu
;
316 menuText
->Append(TEXT_ADD_SOME
, "&Append some text\tCtrl-A");
317 menuText
->Append(TEXT_ADD_FREEZE
, "&Append text with freeze/thaw\tShift-Ctrl-A");
318 menuText
->Append(TEXT_MOVE_ENDTEXT
, "Move cursor to the end of &text");
319 menuText
->Append(TEXT_MOVE_ENDENTRY
, "Move cursor to the end of &entry");
320 menuText
->Append(TEXT_SET_EDITABLE
, "Toggle &editable state", "", TRUE
);
321 menuText
->Append(TEXT_SET_ENABLED
, "Toggle e&nabled state", "", TRUE
);
322 menuText
->Check(TEXT_SET_EDITABLE
, TRUE
);
323 menuText
->Check(TEXT_SET_ENABLED
, TRUE
);
324 menuText
->AppendSeparator();
325 menuText
->Append(TEXT_LINE_DOWN
, "Scroll text one line down");
326 menuText
->Append(TEXT_LINE_UP
, "Scroll text one line up");
327 menuText
->Append(TEXT_PAGE_DOWN
, "Scroll text one page down");
328 menuText
->Append(TEXT_PAGE_DOWN
, "Scroll text one page up");
329 menu_bar
->Append(menuText
, "Te&xt");
331 wxMenu
*menuLog
= new wxMenu
;
332 menuLog
->Append(TEXT_LOG_KEY
, "Log &key events", "", TRUE
);
333 menuLog
->Append(TEXT_LOG_CHAR
, "Log &char events", "", TRUE
);
334 menuLog
->Append(TEXT_LOG_MOUSE
, "Log &mouse events", "", TRUE
);
335 menuLog
->Append(TEXT_LOG_TEXT
, "Log &text events", "", TRUE
);
336 menuLog
->AppendSeparator();
337 menuLog
->Append(TEXT_CLEAR
, "&Clear the log\tCtrl-C",
338 "Clear the log window contents");
339 menuLog
->Check(TEXT_LOG_KEY
, TRUE
);
340 menuLog
->Check(TEXT_LOG_CHAR
, TRUE
);
341 menuLog
->Check(TEXT_LOG_TEXT
, TRUE
);
342 menu_bar
->Append(menuLog
, "&Log");
344 frame
->SetMenuBar(menu_bar
);
354 //----------------------------------------------------------------------
356 //----------------------------------------------------------------------
358 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
359 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
360 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
361 EVT_CHAR(MyTextCtrl::OnChar
)
363 EVT_TEXT(-1, MyTextCtrl::OnText
)
364 EVT_TEXT_URL(-1, MyTextCtrl::OnTextURL
)
365 EVT_TEXT_MAXLEN(-1, MyTextCtrl::OnTextMaxLen
)
367 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
370 bool MyTextCtrl::ms_logKey
= TRUE
;
371 bool MyTextCtrl::ms_logChar
= TRUE
;
372 bool MyTextCtrl::ms_logMouse
= FALSE
;
373 bool MyTextCtrl::ms_logText
= TRUE
;
375 void MyTextCtrl::LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const
378 long keycode
= event
.KeyCode();
382 case WXK_BACK
: key
= "BACK"; break;
383 case WXK_TAB
: key
= "TAB"; break;
384 case WXK_RETURN
: key
= "RETURN"; break;
385 case WXK_ESCAPE
: key
= "ESCAPE"; break;
386 case WXK_SPACE
: key
= "SPACE"; break;
387 case WXK_DELETE
: key
= "DELETE"; break;
388 case WXK_START
: key
= "START"; break;
389 case WXK_LBUTTON
: key
= "LBUTTON"; break;
390 case WXK_RBUTTON
: key
= "RBUTTON"; break;
391 case WXK_CANCEL
: key
= "CANCEL"; break;
392 case WXK_MBUTTON
: key
= "MBUTTON"; break;
393 case WXK_CLEAR
: key
= "CLEAR"; break;
394 case WXK_SHIFT
: key
= "SHIFT"; break;
395 case WXK_ALT
: key
= "ALT"; break;
396 case WXK_CONTROL
: key
= "CONTROL"; break;
397 case WXK_MENU
: key
= "MENU"; break;
398 case WXK_PAUSE
: key
= "PAUSE"; break;
399 case WXK_CAPITAL
: key
= "CAPITAL"; break;
400 case WXK_PRIOR
: key
= "PRIOR"; break;
401 case WXK_NEXT
: key
= "NEXT"; break;
402 case WXK_END
: key
= "END"; break;
403 case WXK_HOME
: key
= "HOME"; break;
404 case WXK_LEFT
: key
= "LEFT"; break;
405 case WXK_UP
: key
= "UP"; break;
406 case WXK_RIGHT
: key
= "RIGHT"; break;
407 case WXK_DOWN
: key
= "DOWN"; break;
408 case WXK_SELECT
: key
= "SELECT"; break;
409 case WXK_PRINT
: key
= "PRINT"; break;
410 case WXK_EXECUTE
: key
= "EXECUTE"; break;
411 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
412 case WXK_INSERT
: key
= "INSERT"; break;
413 case WXK_HELP
: key
= "HELP"; break;
414 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
415 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
416 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
417 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
418 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
419 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
420 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
421 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
422 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
423 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
424 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
425 case WXK_ADD
: key
= "ADD"; break;
426 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
427 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
428 case WXK_DECIMAL
: key
= "DECIMAL"; break;
429 case WXK_DIVIDE
: key
= "DIVIDE"; break;
430 case WXK_F1
: key
= "F1"; break;
431 case WXK_F2
: key
= "F2"; break;
432 case WXK_F3
: key
= "F3"; break;
433 case WXK_F4
: key
= "F4"; break;
434 case WXK_F5
: key
= "F5"; break;
435 case WXK_F6
: key
= "F6"; break;
436 case WXK_F7
: key
= "F7"; break;
437 case WXK_F8
: key
= "F8"; break;
438 case WXK_F9
: key
= "F9"; break;
439 case WXK_F10
: key
= "F10"; break;
440 case WXK_F11
: key
= "F11"; break;
441 case WXK_F12
: key
= "F12"; break;
442 case WXK_F13
: key
= "F13"; break;
443 case WXK_F14
: key
= "F14"; break;
444 case WXK_F15
: key
= "F15"; break;
445 case WXK_F16
: key
= "F16"; break;
446 case WXK_F17
: key
= "F17"; break;
447 case WXK_F18
: key
= "F18"; break;
448 case WXK_F19
: key
= "F19"; break;
449 case WXK_F20
: key
= "F20"; break;
450 case WXK_F21
: key
= "F21"; break;
451 case WXK_F22
: key
= "F22"; break;
452 case WXK_F23
: key
= "F23"; break;
453 case WXK_F24
: key
= "F24"; break;
454 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
455 case WXK_SCROLL
: key
= "SCROLL"; break;
456 case WXK_PAGEUP
: key
= "PAGEUP"; break;
457 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
458 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
459 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
460 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
461 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
462 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
463 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
464 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
465 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
466 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
467 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
468 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
469 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
470 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
471 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
472 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
473 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
474 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
475 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
476 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
477 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
478 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
479 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
480 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
481 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
482 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
486 if ( wxIsprint((int)keycode
) )
487 key
.Printf(_T("'%c'"), (char)keycode
);
488 else if ( keycode
> 0 && keycode
< 27 )
489 key
.Printf(_("Ctrl-%c"), _T('A') + keycode
- 1);
491 key
.Printf(_T("unknown (%ld)"), keycode
);
496 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
499 GetChar( event
.ControlDown(), _T('C') ),
500 GetChar( event
.AltDown(), _T('A') ),
501 GetChar( event
.ShiftDown(), _T('S') ),
502 GetChar( event
.MetaDown(), _T('M') ) );
505 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
510 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
513 dbl
= ev
.LeftDClick();
516 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
518 button
= _T("Middle");
519 dbl
= ev
.MiddleDClick();
522 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
524 button
= _T("Right");
525 dbl
= ev
.RightDClick();
530 return _T("Unknown mouse event");
533 return wxString::Format(_T("%s mouse button %s"),
535 dbl
? _T("double clicked")
536 : up
? _T("released") : _T("clicked"));
539 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
551 msg
= _T("Mouse entered the window");
553 else if ( ev
.Leaving() )
555 msg
= _T("Mouse left the window");
559 msg
= GetMouseEventDesc(ev
);
562 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ")
564 << GetChar( ev
.LeftIsDown(), _T('1') )
565 << GetChar( ev
.MiddleIsDown(), _T('2') )
566 << GetChar( ev
.RightIsDown(), _T('3') )
567 << GetChar( ev
.ControlDown(), _T('C') )
568 << GetChar( ev
.AltDown(), _T('A') )
569 << GetChar( ev
.ShiftDown(), _T('S') )
570 << GetChar( ev
.MetaDown(), _T('M') );
574 //else: we're not interested in mouse move events
577 void MyTextCtrl::OnText(wxCommandEvent
& event
)
582 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
583 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
586 wxLogMessage(_T("Text changed in control '%s'"), data
);
590 wxLogMessage(_T("Text changed in some control"));
594 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& event
)
596 wxLogMessage(_T("You can't enter more characters into this control."));
599 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
601 const wxMouseEvent
& ev
= event
.GetMouseEvent();
603 // filter out mouse moves, too many of them
607 long start
= event
.GetURLStart(),
608 end
= event
.GetURLEnd();
610 wxLogMessage(_T("Mouse event over URL '%s': %s"),
611 GetValue().Mid(start
, end
- start
).c_str(),
612 GetMouseEventDesc(ev
).c_str());
615 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
618 LogEvent( _T("Char"), event
);
623 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
626 LogEvent( _T("Key up"), event
);
631 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
633 switch ( event
.KeyCode() )
636 // show current position and text length
638 long line
, column
, pos
= GetInsertionPoint();
639 PositionToXY(pos
, &column
, &line
);
641 wxLogMessage( _T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
653 SetInsertionPointEnd();
658 SetInsertionPoint(10);
664 wxLogDebug( wxT("Now capturing mouse and events.") );
670 wxLogDebug( wxT("Stopped capturing mouse and events.") );
677 // insert a blank line
682 SetValue("F6 was just pressed.");
692 GetSelection(&from
, &to
);
694 wxString sel
= GetStringSelection();
696 wxLogMessage(_T("Selection: from %ld to %ld."), from
, to
);
697 wxLogMessage(_T("Selection = '%s' (len = %u)"),
698 sel
.c_str(), sel
.length());
703 LogEvent( wxT("Key down"), event
);
708 //----------------------------------------------------------------------
710 //----------------------------------------------------------------------
712 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
713 EVT_SIZE(MyPanel::OnSize
)
716 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
717 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
719 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE
);
721 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
725 // single line text controls
727 m_text
= new MyTextCtrl( this, -1, "Single line.",
728 wxPoint(10,10), wxSize(140,-1),
730 m_text
->SetForegroundColour(*wxBLUE
);
731 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
732 (*m_text
) << " Appended.";
733 m_text
->SetInsertionPoint(0);
734 m_text
->WriteText( "Prepended. " );
736 m_password
= new MyTextCtrl( this, -1, "",
737 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
739 m_readonly
= new MyTextCtrl( this, -1, "Read only",
740 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
742 m_limited
= new MyTextCtrl(this, -1, "Max 8 ch",
743 wxPoint(10, 130), wxSize(140, -1));
744 m_limited
->SetMaxLength(8);
746 // multi line text controls
748 m_horizontal
= new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
749 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
751 // a little hack to use the command line argument for encoding testing
752 if ( wxTheApp
->argc
== 2 )
754 switch ( wxTheApp
->argv
[1][0] )
757 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
759 wxFONTENCODING_ISO8859_2
));
760 m_horizontal
->SetValue("®lu»ouèký kùò zbìsile èe¹tina «»");
764 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
766 wxFONTENCODING_KOI8
));
767 m_horizontal
->SetValue("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
772 m_horizontal
->SetValue("Text in default encoding");
775 m_multitext
= new MyTextCtrl( this, -1, "Multi line.",
776 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
777 m_multitext
->SetFont(*wxITALIC_FONT
);
778 (*m_multitext
) << " Appended.";
779 m_multitext
->SetInsertionPoint(0);
780 m_multitext
->WriteText( "Prepended. " );
781 m_multitext
->SetForegroundColour(*wxRED
);
782 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
785 m_multitext
->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
788 m_tab
= new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
789 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
790 m_tab
->SetClientData((void *)_T("tab"));
792 m_enter
= new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
793 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
794 m_enter
->SetClientData((void *)_T("enter"));
796 m_textrich
= new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
797 "(even under broken Win9x)\n"
798 "and a very very very very very "
799 "very very very long line to test "
801 wxPoint(450, 10), wxSize(230, 230),
808 m_textrich
->SetStyle(0, 10, *wxRED
);
809 m_textrich
->SetStyle(10, 20, *wxBLUE
);
810 m_textrich
->SetStyle(30, 40,
811 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
812 m_textrich
->SetDefaultStyle(wxTextAttr());
813 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
814 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
815 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
816 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
817 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
818 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
819 m_textrich
->SetDefaultStyle(*wxBLUE
);
820 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
821 _T("type should be in blue as well"));
823 m_textrich
->SetFont(wxFont(12, wxFONTFAMILY_TELETYPE
,
824 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
825 m_textrich
->SetDefaultStyle(wxTextAttr(*wxRED
));
826 m_textrich
->AppendText(_T("Red text\n"));
827 m_textrich
->SetDefaultStyle(wxTextAttr(wxNullColour
, *wxLIGHT_GREY
));
828 m_textrich
->AppendText(_T("Red on grey text\n"));
829 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
));
830 m_textrich
->AppendText(_T("Blue on grey text\n"));
834 void MyPanel::OnSize( wxSizeEvent
&event
)
836 wxSize
client_area( GetClientSize() );
837 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
842 void MyPanel::DoPasteFromClipboard()
844 // On X11, we want to get the data from the primary selection instead
845 // of the normal clipboard (which isn't normal under X11 at all). This
846 // call has no effect under MSW.
847 wxTheClipboard
->UsePrimarySelection();
849 if (!wxTheClipboard
->Open())
851 *m_log
<< "Error opening the clipboard.\n";
856 *m_log
<< "Successfully opened the clipboard.\n";
859 wxTextDataObject data
;
861 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
863 *m_log
<< "Clipboard supports requested format.\n";
865 if (wxTheClipboard
->GetData( data
))
867 *m_log
<< "Successfully retrieved data from the clipboard.\n";
868 *m_multitext
<< data
.GetText() << "\n";
872 *m_log
<< "Error getting data from the clipboard.\n";
877 *m_log
<< "Clipboard doesn't support requested format.\n";
880 wxTheClipboard
->Close();
882 *m_log
<< "Closed the clipboard.\n";
885 void MyPanel::DoCopyToClipboard()
887 // On X11, we want to get the data from the primary selection instead
888 // of the normal clipboard (which isn't normal under X11 at all). This
889 // call has no effect under MSW.
890 wxTheClipboard
->UsePrimarySelection();
892 wxString
text( m_multitext
->GetLineText(0) );
896 *m_log
<< "No text to copy.\n";
901 if (!wxTheClipboard
->Open())
903 *m_log
<< "Error opening the clipboard.\n";
909 *m_log
<< "Successfully opened the clipboard.\n";
912 wxTextDataObject
*data
= new wxTextDataObject( text
);
914 if (!wxTheClipboard
->SetData( data
))
916 *m_log
<< "Error while copying to the clipboard.\n";
920 *m_log
<< "Successfully copied data to the clipboard.\n";
923 wxTheClipboard
->Close();
925 *m_log
<< "Closed the clipboard.\n";
928 #endif // wxUSE_CLIPBOARD
930 void MyPanel::DoMoveToEndOfText()
932 m_multitext
->SetInsertionPointEnd();
933 m_multitext
->SetFocus();
936 void MyPanel::DoMoveToEndOfEntry()
938 m_text
->SetInsertionPointEnd();
942 //----------------------------------------------------------------------
944 //----------------------------------------------------------------------
946 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
947 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
948 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
949 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
950 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
952 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
953 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
954 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
955 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
956 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
959 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
960 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
961 #endif // wxUSE_TOOLTIPS
964 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
965 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
966 #endif // wxUSE_CLIPBOARD
968 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
969 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
970 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
971 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
973 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
974 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
976 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
977 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
978 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
979 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
981 EVT_IDLE(MyFrame::OnIdle
)
984 MyFrame::MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
)
985 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
) )
989 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
992 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
997 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1001 wxMessageDialog
dialog(this,
1002 "This is a text control sample. It demonstrates the many different\n"
1003 "text control styles, the use of the clipboard, setting and handling\n"
1004 "tooltips and intercepting key and char events.\n"
1006 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
1007 "About wxTextCtrl Sample",
1008 wxOK
| wxICON_INFORMATION
);
1016 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
1018 static long s_delay
= 5000;
1021 delay
.Printf( _T("%ld"), s_delay
);
1023 delay
= wxGetTextFromUser("Enter delay (in milliseconds)",
1024 "Set tooltip delay",
1028 return; // cancelled
1030 wxSscanf(delay
, _T("%ld"), &s_delay
);
1032 wxToolTip::SetDelay(s_delay
);
1034 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
1037 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
1039 static bool s_enabled
= TRUE
;
1041 s_enabled
= !s_enabled
;
1043 wxToolTip::Enable(s_enabled
);
1045 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
1049 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1051 m_panel
->m_log
->Clear();
1054 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1056 static bool s_editable
= TRUE
;
1058 s_editable
= !s_editable
;
1059 m_panel
->m_text
->SetEditable(s_editable
);
1060 m_panel
->m_password
->SetEditable(s_editable
);
1061 m_panel
->m_multitext
->SetEditable(s_editable
);
1062 m_panel
->m_textrich
->SetEditable(s_editable
);
1065 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1067 bool enabled
= m_panel
->m_text
->IsEnabled();
1070 m_panel
->m_text
->Enable(enabled
);
1071 m_panel
->m_password
->Enable(enabled
);
1072 m_panel
->m_multitext
->Enable(enabled
);
1073 m_panel
->m_readonly
->Enable(enabled
);
1074 m_panel
->m_limited
->Enable(enabled
);
1075 m_panel
->m_textrich
->Enable(enabled
);
1078 void MyFrame::OnFileSave(wxCommandEvent
& event
)
1080 if ( m_panel
->m_textrich
->SaveFile("dummy.txt") )
1083 // verify that the fil length is correct (it wasn't under Win95)
1084 wxFile
file(wxT("dummy.txt"));
1085 wxLogStatus(this, _T("Successfully saved file (text len = %ld, file size = %ld)"),
1086 m_panel
->m_textrich
->GetValue().length(),
1091 wxLogStatus(this, _T("Couldn't save the file"));
1094 void MyFrame::OnFileLoad(wxCommandEvent
& event
)
1096 if ( m_panel
->m_textrich
->LoadFile("dummy.txt") )
1097 wxLogStatus(this, _T("Successfully loaded file"));
1099 wxLogStatus(this, _T("Couldn't load the file"));
1102 void MyFrame::OnIdle( wxIdleEvent
& event
)
1104 // track the window which has the focus in the status bar
1105 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1106 wxWindow
*focus
= wxWindow::FindFocus();
1107 if ( focus
&& (focus
!= s_windowFocus
) )
1109 s_windowFocus
= focus
;
1114 _T("Focus: wxWindow = %p, HWND = %08x"),
1116 _T("Focus: wxWindow = %p"),
1120 , s_windowFocus
->GetHWND()