]>
git.saurik.com Git - wxWidgets.git/blob - samples/text/text.cpp
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
104 void DoMoveToEndOfText();
105 void DoMoveToEndOfEntry();
107 void OnSize( wxSizeEvent
&event
);
110 MyTextCtrl
*m_password
;
113 MyTextCtrl
*m_readonly
;
114 MyTextCtrl
*m_limited
;
116 MyTextCtrl
*m_multitext
;
117 MyTextCtrl
*m_horizontal
;
119 MyTextCtrl
*m_textrich
;
124 // get the currently focused text control or return the default one is no
125 // text ctrl has focus
126 wxTextCtrl
*GetFocusedText(wxTextCtrl
*textDef
);
128 DECLARE_EVENT_TABLE()
131 class MyFrame
: public wxFrame
134 MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
);
136 void OnQuit(wxCommandEvent
& event
);
137 void OnAbout(wxCommandEvent
& event
);
139 void OnSetTooltipDelay(wxCommandEvent
& event
);
140 void OnToggleTooltips(wxCommandEvent
& event
);
141 #endif // wxUSE_TOOLTIPS
144 void OnPasteFromClipboard( wxCommandEvent
&event
)
145 { m_panel
->DoPasteFromClipboard(); }
146 void OnCopyToClipboard( wxCommandEvent
&event
)
147 { m_panel
->DoCopyToClipboard(); }
148 #endif // wxUSE_CLIPBOARD
150 void OnAddTextFreeze( wxCommandEvent
& event
)
152 void OnAddText( wxCommandEvent
& event
)
153 { DoAddText(false); }
154 void OnRemoveText( wxCommandEvent
& event
)
155 { m_panel
->DoRemoveText(); }
157 void OnMoveToEndOfText( wxCommandEvent
&event
)
158 { m_panel
->DoMoveToEndOfText(); }
159 void OnMoveToEndOfEntry( wxCommandEvent
&event
)
160 { m_panel
->DoMoveToEndOfEntry(); }
162 void OnScrollLineDown(wxCommandEvent
& event
)
164 if ( !m_panel
->m_textrich
->LineDown() )
165 wxLogMessage(_T("Already at the bottom"));
168 void OnScrollLineUp(wxCommandEvent
& event
)
170 if ( !m_panel
->m_textrich
->LineUp() )
171 wxLogMessage(_T("Already at the top"));
174 void OnScrollPageDown(wxCommandEvent
& event
)
176 if ( !m_panel
->m_textrich
->PageDown() )
177 wxLogMessage(_T("Already at the bottom"));
180 void OnScrollPageUp(wxCommandEvent
& event
)
182 if ( !m_panel
->m_textrich
->PageUp() )
183 wxLogMessage(_T("Already at the top"));
186 void OnLogClear(wxCommandEvent
& event
);
187 void OnFileSave(wxCommandEvent
& event
);
188 void OnFileLoad(wxCommandEvent
& event
);
190 void OnSetEditable(wxCommandEvent
& event
);
191 void OnSetEnabled(wxCommandEvent
& event
);
193 void OnLogKey(wxCommandEvent
& event
)
195 MyTextCtrl::ms_logKey
= event
.IsChecked();
198 void OnLogChar(wxCommandEvent
& event
)
200 MyTextCtrl::ms_logChar
= event
.IsChecked();
203 void OnLogMouse(wxCommandEvent
& event
)
205 MyTextCtrl::ms_logMouse
= event
.IsChecked();
208 void OnLogText(wxCommandEvent
& event
)
210 MyTextCtrl::ms_logText
= event
.IsChecked();
213 void OnIdle( wxIdleEvent
& event
);
216 void DoAddText(bool freeze
)
218 wxTextCtrl
*text
= m_panel
->m_textrich
;
222 for ( int i
= 0; i
< 100; i
++ )
223 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
225 text
->SetInsertionPoint(0);
233 DECLARE_EVENT_TABLE()
236 //----------------------------------------------------------------------
238 //----------------------------------------------------------------------
242 //----------------------------------------------------------------------
244 //----------------------------------------------------------------------
255 TEXT_CLIPBOARD_COPY
= 200,
256 TEXT_CLIPBOARD_PASTE
,
259 TEXT_TOOLTIPS_SETDELAY
= 300,
260 TEXT_TOOLTIPS_ENABLE
,
286 // Create the main frame window
287 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
288 "Text wxWindows sample", 50, 50, 700, 420);
289 frame
->SetSizeHints( 500, 400 );
291 wxMenu
*file_menu
= new wxMenu
;
292 file_menu
->Append(TEXT_SAVE
, "&Save file\tCtrl-S",
293 "Save the text control contents to file");
294 file_menu
->Append(TEXT_LOAD
, "&Load file\tCtrl-O",
295 "Load the sample file into text control");
296 file_menu
->AppendSeparator();
297 file_menu
->Append(TEXT_ABOUT
, "&About\tAlt-A");
298 file_menu
->AppendSeparator();
299 file_menu
->Append(TEXT_QUIT
, "E&xit\tAlt-X", "Quit this sample");
301 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
302 menu_bar
->Append(file_menu
, "&File");
305 wxMenu
*tooltip_menu
= new wxMenu
;
306 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, "Set &delay\tCtrl-D");
307 tooltip_menu
->AppendSeparator();
308 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, "&Toggle tooltips\tCtrl-T",
309 "enable/disable tooltips", TRUE
);
310 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, TRUE
);
311 menu_bar
->Append(tooltip_menu
, "&Tooltips");
312 #endif // wxUSE_TOOLTIPS
315 wxMenu
*menuClipboard
= new wxMenu
;
316 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, "&Copy\tCtrl-C",
317 "Copy the first line to the clipboard");
318 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, "&Paste\tCtrl-V",
319 "Paste from clipboard to the text control");
320 menu_bar
->Append(menuClipboard
, "&Clipboard");
321 #endif // wxUSE_CLIPBOARD
323 wxMenu
*menuText
= new wxMenu
;
324 menuText
->Append(TEXT_ADD_SOME
, "&Append some text\tCtrl-A");
325 menuText
->Append(TEXT_ADD_FREEZE
, "&Append text with freeze/thaw\tShift-Ctrl-A");
326 menuText
->Append(TEXT_REMOVE
, "&Remove first 10 characters\tCtrl-X");
327 menuText
->AppendSeparator();
328 menuText
->Append(TEXT_MOVE_ENDTEXT
, "Move cursor to the end of &text");
329 menuText
->Append(TEXT_MOVE_ENDENTRY
, "Move cursor to the end of &entry");
330 menuText
->Append(TEXT_SET_EDITABLE
, "Toggle &editable state", "", TRUE
);
331 menuText
->Append(TEXT_SET_ENABLED
, "Toggle e&nabled state", "", TRUE
);
332 menuText
->Check(TEXT_SET_EDITABLE
, TRUE
);
333 menuText
->Check(TEXT_SET_ENABLED
, TRUE
);
334 menuText
->AppendSeparator();
335 menuText
->Append(TEXT_LINE_DOWN
, "Scroll text one line down");
336 menuText
->Append(TEXT_LINE_UP
, "Scroll text one line up");
337 menuText
->Append(TEXT_PAGE_DOWN
, "Scroll text one page down");
338 menuText
->Append(TEXT_PAGE_DOWN
, "Scroll text one page up");
339 menu_bar
->Append(menuText
, "Te&xt");
341 wxMenu
*menuLog
= new wxMenu
;
342 menuLog
->Append(TEXT_LOG_KEY
, "Log &key events", "", TRUE
);
343 menuLog
->Append(TEXT_LOG_CHAR
, "Log &char events", "", TRUE
);
344 menuLog
->Append(TEXT_LOG_MOUSE
, "Log &mouse events", "", TRUE
);
345 menuLog
->Append(TEXT_LOG_TEXT
, "Log &text events", "", TRUE
);
346 menuLog
->AppendSeparator();
347 menuLog
->Append(TEXT_CLEAR
, "&Clear the log\tCtrl-C",
348 "Clear the log window contents");
349 menuLog
->Check(TEXT_LOG_KEY
, TRUE
);
350 menuLog
->Check(TEXT_LOG_CHAR
, TRUE
);
351 menuLog
->Check(TEXT_LOG_TEXT
, TRUE
);
352 menu_bar
->Append(menuLog
, "&Log");
354 frame
->SetMenuBar(menu_bar
);
364 //----------------------------------------------------------------------
366 //----------------------------------------------------------------------
368 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
369 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
370 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
371 EVT_CHAR(MyTextCtrl::OnChar
)
373 EVT_TEXT(-1, MyTextCtrl::OnText
)
374 EVT_TEXT_URL(-1, MyTextCtrl::OnTextURL
)
375 EVT_TEXT_MAXLEN(-1, MyTextCtrl::OnTextMaxLen
)
377 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
380 bool MyTextCtrl::ms_logKey
= TRUE
;
381 bool MyTextCtrl::ms_logChar
= TRUE
;
382 bool MyTextCtrl::ms_logMouse
= FALSE
;
383 bool MyTextCtrl::ms_logText
= TRUE
;
385 void MyTextCtrl::LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const
388 long keycode
= event
.KeyCode();
392 case WXK_BACK
: key
= "BACK"; break;
393 case WXK_TAB
: key
= "TAB"; break;
394 case WXK_RETURN
: key
= "RETURN"; break;
395 case WXK_ESCAPE
: key
= "ESCAPE"; break;
396 case WXK_SPACE
: key
= "SPACE"; break;
397 case WXK_DELETE
: key
= "DELETE"; break;
398 case WXK_START
: key
= "START"; break;
399 case WXK_LBUTTON
: key
= "LBUTTON"; break;
400 case WXK_RBUTTON
: key
= "RBUTTON"; break;
401 case WXK_CANCEL
: key
= "CANCEL"; break;
402 case WXK_MBUTTON
: key
= "MBUTTON"; break;
403 case WXK_CLEAR
: key
= "CLEAR"; break;
404 case WXK_SHIFT
: key
= "SHIFT"; break;
405 case WXK_ALT
: key
= "ALT"; break;
406 case WXK_CONTROL
: key
= "CONTROL"; break;
407 case WXK_MENU
: key
= "MENU"; break;
408 case WXK_PAUSE
: key
= "PAUSE"; break;
409 case WXK_CAPITAL
: key
= "CAPITAL"; break;
410 case WXK_PRIOR
: key
= "PRIOR"; break;
411 case WXK_NEXT
: key
= "NEXT"; break;
412 case WXK_END
: key
= "END"; break;
413 case WXK_HOME
: key
= "HOME"; break;
414 case WXK_LEFT
: key
= "LEFT"; break;
415 case WXK_UP
: key
= "UP"; break;
416 case WXK_RIGHT
: key
= "RIGHT"; break;
417 case WXK_DOWN
: key
= "DOWN"; break;
418 case WXK_SELECT
: key
= "SELECT"; break;
419 case WXK_PRINT
: key
= "PRINT"; break;
420 case WXK_EXECUTE
: key
= "EXECUTE"; break;
421 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
422 case WXK_INSERT
: key
= "INSERT"; break;
423 case WXK_HELP
: key
= "HELP"; break;
424 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
425 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
426 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
427 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
428 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
429 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
430 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
431 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
432 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
433 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
434 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
435 case WXK_ADD
: key
= "ADD"; break;
436 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
437 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
438 case WXK_DECIMAL
: key
= "DECIMAL"; break;
439 case WXK_DIVIDE
: key
= "DIVIDE"; break;
440 case WXK_F1
: key
= "F1"; break;
441 case WXK_F2
: key
= "F2"; break;
442 case WXK_F3
: key
= "F3"; break;
443 case WXK_F4
: key
= "F4"; break;
444 case WXK_F5
: key
= "F5"; break;
445 case WXK_F6
: key
= "F6"; break;
446 case WXK_F7
: key
= "F7"; break;
447 case WXK_F8
: key
= "F8"; break;
448 case WXK_F9
: key
= "F9"; break;
449 case WXK_F10
: key
= "F10"; break;
450 case WXK_F11
: key
= "F11"; break;
451 case WXK_F12
: key
= "F12"; break;
452 case WXK_F13
: key
= "F13"; break;
453 case WXK_F14
: key
= "F14"; break;
454 case WXK_F15
: key
= "F15"; break;
455 case WXK_F16
: key
= "F16"; break;
456 case WXK_F17
: key
= "F17"; break;
457 case WXK_F18
: key
= "F18"; break;
458 case WXK_F19
: key
= "F19"; break;
459 case WXK_F20
: key
= "F20"; break;
460 case WXK_F21
: key
= "F21"; break;
461 case WXK_F22
: key
= "F22"; break;
462 case WXK_F23
: key
= "F23"; break;
463 case WXK_F24
: key
= "F24"; break;
464 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
465 case WXK_SCROLL
: key
= "SCROLL"; break;
466 case WXK_PAGEUP
: key
= "PAGEUP"; break;
467 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
468 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
469 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
470 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
471 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
472 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
473 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
474 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
475 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
476 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
477 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
478 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
479 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
480 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
481 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
482 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
483 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
484 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
485 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
486 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
487 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
488 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
489 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
490 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
491 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
492 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
496 if ( wxIsprint((int)keycode
) )
497 key
.Printf(_T("'%c'"), (char)keycode
);
498 else if ( keycode
> 0 && keycode
< 27 )
499 key
.Printf(_("Ctrl-%c"), _T('A') + keycode
- 1);
501 key
.Printf(_T("unknown (%ld)"), keycode
);
506 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
509 GetChar( event
.ControlDown(), _T('C') ),
510 GetChar( event
.AltDown(), _T('A') ),
511 GetChar( event
.ShiftDown(), _T('S') ),
512 GetChar( event
.MetaDown(), _T('M') ) );
515 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
520 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
523 dbl
= ev
.LeftDClick();
526 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
528 button
= _T("Middle");
529 dbl
= ev
.MiddleDClick();
532 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
534 button
= _T("Right");
535 dbl
= ev
.RightDClick();
540 return _T("Unknown mouse event");
543 return wxString::Format(_T("%s mouse button %s"),
545 dbl
? _T("double clicked")
546 : up
? _T("released") : _T("clicked"));
549 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
561 msg
= _T("Mouse entered the window");
563 else if ( ev
.Leaving() )
565 msg
= _T("Mouse left the window");
569 msg
= GetMouseEventDesc(ev
);
572 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ")
574 << GetChar( ev
.LeftIsDown(), _T('1') )
575 << GetChar( ev
.MiddleIsDown(), _T('2') )
576 << GetChar( ev
.RightIsDown(), _T('3') )
577 << GetChar( ev
.ControlDown(), _T('C') )
578 << GetChar( ev
.AltDown(), _T('A') )
579 << GetChar( ev
.ShiftDown(), _T('S') )
580 << GetChar( ev
.MetaDown(), _T('M') );
584 //else: we're not interested in mouse move events
587 void MyTextCtrl::OnText(wxCommandEvent
& event
)
592 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
593 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
596 wxLogMessage(_T("Text changed in control '%s'"), data
);
600 wxLogMessage(_T("Text changed in some control"));
604 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& event
)
606 wxLogMessage(_T("You can't enter more characters into this control."));
609 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
611 const wxMouseEvent
& ev
= event
.GetMouseEvent();
613 // filter out mouse moves, too many of them
617 long start
= event
.GetURLStart(),
618 end
= event
.GetURLEnd();
620 wxLogMessage(_T("Mouse event over URL '%s': %s"),
621 GetValue().Mid(start
, end
- start
).c_str(),
622 GetMouseEventDesc(ev
).c_str());
625 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
628 LogEvent( _T("Char"), event
);
633 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
636 LogEvent( _T("Key up"), event
);
641 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
643 switch ( event
.KeyCode() )
646 // show current position and text length
648 long line
, column
, pos
= GetInsertionPoint();
649 PositionToXY(pos
, &column
, &line
);
651 wxLogMessage( _T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
663 SetInsertionPointEnd();
668 SetInsertionPoint(10);
674 wxLogDebug( wxT("Now capturing mouse and events.") );
680 wxLogDebug( wxT("Stopped capturing mouse and events.") );
687 // insert a blank line
692 SetValue("F6 was just pressed.");
702 GetSelection(&from
, &to
);
704 wxString sel
= GetStringSelection();
706 wxLogMessage(_T("Selection: from %ld to %ld."), from
, to
);
707 wxLogMessage(_T("Selection = '%s' (len = %u)"),
708 sel
.c_str(), sel
.length());
713 LogEvent( wxT("Key down"), event
);
718 //----------------------------------------------------------------------
720 //----------------------------------------------------------------------
722 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
723 EVT_SIZE(MyPanel::OnSize
)
726 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
727 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
729 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE
);
731 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
735 // single line text controls
737 m_text
= new MyTextCtrl( this, -1, "Single line.",
738 wxPoint(10,10), wxSize(140,-1),
740 m_text
->SetForegroundColour(*wxBLUE
);
741 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
742 (*m_text
) << " Appended.";
743 m_text
->SetInsertionPoint(0);
744 m_text
->WriteText( "Prepended. " );
746 m_password
= new MyTextCtrl( this, -1, "",
747 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
749 m_readonly
= new MyTextCtrl( this, -1, "Read only",
750 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
752 m_limited
= new MyTextCtrl(this, -1, "Max 8 ch",
753 wxPoint(10, 130), wxSize(140, -1));
754 m_limited
->SetMaxLength(8);
756 // multi line text controls
758 m_horizontal
= new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
759 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
761 // a little hack to use the command line argument for encoding testing
762 if ( wxTheApp
->argc
== 2 )
764 switch ( wxTheApp
->argv
[1][0] )
767 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
769 wxFONTENCODING_ISO8859_2
));
770 m_horizontal
->SetValue("®lu»ouèký kùò zbìsile èe¹tina «»");
774 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
776 wxFONTENCODING_CP1251
));
777 m_horizontal
->SetValue("Ïðèâåò!");
781 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
783 wxFONTENCODING_CP1251
));
784 m_horizontal
->SetValue("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
789 m_horizontal
->SetValue("Text in default encoding");
792 m_multitext
= new MyTextCtrl( this, -1, "Multi line.",
793 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
794 m_multitext
->SetFont(*wxITALIC_FONT
);
795 (*m_multitext
) << " Appended.";
796 m_multitext
->SetInsertionPoint(0);
797 m_multitext
->WriteText( "Prepended. " );
798 m_multitext
->SetForegroundColour(*wxRED
);
799 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
802 m_multitext
->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
805 m_tab
= new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
806 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
807 m_tab
->SetClientData((void *)_T("tab"));
809 m_enter
= new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
810 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
811 m_enter
->SetClientData((void *)_T("enter"));
813 m_textrich
= new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
814 "(even under broken Win9x)\n"
815 "and a very very very very very "
816 "very very very long line to test "
818 wxPoint(450, 10), wxSize(230, 230),
825 m_textrich
->SetStyle(0, 10, *wxRED
);
826 m_textrich
->SetStyle(10, 20, *wxBLUE
);
827 m_textrich
->SetStyle(30, 40,
828 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
829 m_textrich
->SetDefaultStyle(wxTextAttr());
830 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
831 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
832 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
833 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
834 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
835 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
836 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
, *wxWHITE
));
837 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
838 _T("type should be in blue as well"));
840 m_textrich
->SetFont(wxFont(12, wxFONTFAMILY_TELETYPE
,
841 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
842 m_textrich
->SetDefaultStyle(wxTextAttr(*wxRED
));
843 m_textrich
->AppendText(_T("Red text\n"));
844 m_textrich
->SetDefaultStyle(wxTextAttr(wxNullColour
, *wxLIGHT_GREY
));
845 m_textrich
->AppendText(_T("Red on grey text\n"));
846 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
));
847 m_textrich
->AppendText(_T("Blue on grey text\n"));
851 void MyPanel::OnSize( wxSizeEvent
&event
)
853 wxSize
client_area( GetClientSize() );
854 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
858 wxTextCtrl
*MyPanel::GetFocusedText(wxTextCtrl
*textDef
)
860 wxWindow
*win
= FindFocus();
862 wxTextCtrl
*text
= win
? wxDynamicCast(win
, wxTextCtrl
) : NULL
;
863 return text
? text
: textDef
;
867 void MyPanel::DoPasteFromClipboard()
869 // On X11, we want to get the data from the primary selection instead
870 // of the normal clipboard (which isn't normal under X11 at all). This
871 // call has no effect under MSW.
872 wxTheClipboard
->UsePrimarySelection();
874 if (!wxTheClipboard
->Open())
876 *m_log
<< "Error opening the clipboard.\n";
881 *m_log
<< "Successfully opened the clipboard.\n";
884 wxTextDataObject data
;
886 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
888 *m_log
<< "Clipboard supports requested format.\n";
890 if (wxTheClipboard
->GetData( data
))
892 *m_log
<< "Successfully retrieved data from the clipboard.\n";
893 *m_multitext
<< data
.GetText() << "\n";
897 *m_log
<< "Error getting data from the clipboard.\n";
902 *m_log
<< "Clipboard doesn't support requested format.\n";
905 wxTheClipboard
->Close();
907 *m_log
<< "Closed the clipboard.\n";
910 void MyPanel::DoCopyToClipboard()
912 // On X11, we want to get the data from the primary selection instead
913 // of the normal clipboard (which isn't normal under X11 at all). This
914 // call has no effect under MSW.
915 wxTheClipboard
->UsePrimarySelection();
917 wxString
text( m_multitext
->GetLineText(0) );
921 *m_log
<< "No text to copy.\n";
926 if (!wxTheClipboard
->Open())
928 *m_log
<< "Error opening the clipboard.\n";
934 *m_log
<< "Successfully opened the clipboard.\n";
937 wxTextDataObject
*data
= new wxTextDataObject( text
);
939 if (!wxTheClipboard
->SetData( data
))
941 *m_log
<< "Error while copying to the clipboard.\n";
945 *m_log
<< "Successfully copied data to the clipboard.\n";
948 wxTheClipboard
->Close();
950 *m_log
<< "Closed the clipboard.\n";
953 #endif // wxUSE_CLIPBOARD
955 void MyPanel::DoMoveToEndOfText()
957 m_multitext
->SetInsertionPointEnd();
958 m_multitext
->SetFocus();
961 void MyPanel::DoMoveToEndOfEntry()
963 m_text
->SetInsertionPointEnd();
967 void MyPanel::DoRemoveText()
969 GetFocusedText(m_multitext
)->Remove(0, 10);
972 //----------------------------------------------------------------------
974 //----------------------------------------------------------------------
976 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
977 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
978 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
979 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
980 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
982 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
983 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
984 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
985 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
986 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
989 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
990 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
991 #endif // wxUSE_TOOLTIPS
994 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
995 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
996 #endif // wxUSE_CLIPBOARD
998 EVT_MENU(TEXT_REMOVE
, MyFrame::OnRemoveText
)
999 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
1000 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
1001 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
1002 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
1004 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
1005 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
1007 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
1008 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
1009 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
1010 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
1012 EVT_IDLE(MyFrame::OnIdle
)
1015 MyFrame::MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
)
1016 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
) )
1020 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
1023 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
1028 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1030 wxBeginBusyCursor();
1032 wxMessageDialog
dialog(this,
1033 "This is a text control sample. It demonstrates the many different\n"
1034 "text control styles, the use of the clipboard, setting and handling\n"
1035 "tooltips and intercepting key and char events.\n"
1037 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
1038 "About wxTextCtrl Sample",
1039 wxOK
| wxICON_INFORMATION
);
1047 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
1049 static long s_delay
= 5000;
1052 delay
.Printf( _T("%ld"), s_delay
);
1054 delay
= wxGetTextFromUser("Enter delay (in milliseconds)",
1055 "Set tooltip delay",
1059 return; // cancelled
1061 wxSscanf(delay
, _T("%ld"), &s_delay
);
1063 wxToolTip::SetDelay(s_delay
);
1065 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
1068 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
1070 static bool s_enabled
= TRUE
;
1072 s_enabled
= !s_enabled
;
1074 wxToolTip::Enable(s_enabled
);
1076 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
1080 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1082 m_panel
->m_log
->Clear();
1085 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1087 static bool s_editable
= TRUE
;
1089 s_editable
= !s_editable
;
1090 m_panel
->m_text
->SetEditable(s_editable
);
1091 m_panel
->m_password
->SetEditable(s_editable
);
1092 m_panel
->m_multitext
->SetEditable(s_editable
);
1093 m_panel
->m_textrich
->SetEditable(s_editable
);
1096 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1098 bool enabled
= m_panel
->m_text
->IsEnabled();
1101 m_panel
->m_text
->Enable(enabled
);
1102 m_panel
->m_password
->Enable(enabled
);
1103 m_panel
->m_multitext
->Enable(enabled
);
1104 m_panel
->m_readonly
->Enable(enabled
);
1105 m_panel
->m_limited
->Enable(enabled
);
1106 m_panel
->m_textrich
->Enable(enabled
);
1109 void MyFrame::OnFileSave(wxCommandEvent
& event
)
1111 if ( m_panel
->m_textrich
->SaveFile("dummy.txt") )
1114 // verify that the fil length is correct (it wasn't under Win95)
1115 wxFile
file(wxT("dummy.txt"));
1116 wxLogStatus(this, _T("Successfully saved file (text len = %ld, file size = %ld)"),
1117 m_panel
->m_textrich
->GetValue().length(),
1122 wxLogStatus(this, _T("Couldn't save the file"));
1125 void MyFrame::OnFileLoad(wxCommandEvent
& event
)
1127 if ( m_panel
->m_textrich
->LoadFile("dummy.txt") )
1128 wxLogStatus(this, _T("Successfully loaded file"));
1130 wxLogStatus(this, _T("Couldn't load the file"));
1133 void MyFrame::OnIdle( wxIdleEvent
& event
)
1135 // track the window which has the focus in the status bar
1136 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1137 wxWindow
*focus
= wxWindow::FindFocus();
1138 if ( focus
&& (focus
!= s_windowFocus
) )
1140 s_windowFocus
= focus
;
1145 _T("Focus: wxWindow = %p, HWND = %08x"),
1147 _T("Focus: wxWindow = %p"),
1151 , s_windowFocus
->GetHWND()