]>
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
) { m_hasCapture
= FALSE
; }
66 void OnKeyDown(wxKeyEvent
& event
);
67 void OnKeyUp(wxKeyEvent
& event
);
68 void OnChar(wxKeyEvent
& event
);
69 void OnText(wxCommandEvent
& event
);
70 void OnTextURL(wxTextUrlEvent
& event
);
71 void OnMouseEvent(wxMouseEvent
& event
);
76 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
77 void LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
82 class MyPanel
: public wxPanel
85 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
88 void DoPasteFromClipboard();
89 void DoCopyToClipboard();
90 #endif // wxUSE_CLIPBOARD
92 void DoMoveToEndOfText();
93 void DoMoveToEndOfEntry();
95 void OnSize( wxSizeEvent
&event
);
98 MyTextCtrl
*m_password
;
101 MyTextCtrl
*m_readonly
;
103 MyTextCtrl
*m_multitext
;
104 MyTextCtrl
*m_horizontal
;
106 MyTextCtrl
*m_textrich
;
111 DECLARE_EVENT_TABLE()
114 class MyFrame
: public wxFrame
117 MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
);
119 void OnQuit(wxCommandEvent
& event
);
120 void OnAbout(wxCommandEvent
& event
);
122 void OnSetTooltipDelay(wxCommandEvent
& event
);
123 void OnToggleTooltips(wxCommandEvent
& event
);
124 #endif // wxUSE_TOOLTIPS
127 void OnPasteFromClipboard( wxCommandEvent
&event
)
128 { m_panel
->DoPasteFromClipboard(); }
129 void OnCopyToClipboard( wxCommandEvent
&event
)
130 { m_panel
->DoCopyToClipboard(); }
131 #endif // wxUSE_CLIPBOARD
133 void OnAddTextFreeze( wxCommandEvent
& event
)
135 void OnAddText( wxCommandEvent
& event
)
136 { DoAddText(false); }
138 void OnMoveToEndOfText( wxCommandEvent
&event
)
139 { m_panel
->DoMoveToEndOfText(); }
140 void OnMoveToEndOfEntry( wxCommandEvent
&event
)
141 { m_panel
->DoMoveToEndOfEntry(); }
143 void OnScrollLineDown(wxCommandEvent
& event
)
145 if ( !m_panel
->m_textrich
->LineDown() )
146 wxLogMessage(_T("Already at the bottom"));
149 void OnScrollLineUp(wxCommandEvent
& event
)
151 if ( !m_panel
->m_textrich
->LineUp() )
152 wxLogMessage(_T("Already at the top"));
155 void OnScrollPageDown(wxCommandEvent
& event
)
157 if ( !m_panel
->m_textrich
->PageDown() )
158 wxLogMessage(_T("Already at the bottom"));
161 void OnScrollPageUp(wxCommandEvent
& event
)
163 if ( !m_panel
->m_textrich
->PageUp() )
164 wxLogMessage(_T("Already at the top"));
167 void OnLogClear(wxCommandEvent
& event
);
168 void OnFileSave(wxCommandEvent
& event
);
169 void OnFileLoad(wxCommandEvent
& event
);
171 void OnSetEditable(wxCommandEvent
& event
);
172 void OnSetEnabled(wxCommandEvent
& event
);
174 void OnIdle( wxIdleEvent
& event
);
177 void DoAddText(bool freeze
)
179 wxTextCtrl
*text
= m_panel
->m_textrich
;
183 for ( int i
= 0; i
< 100; i
++ )
184 text
->AppendText(wxString::Format("Line %i\n", i
));
186 text
->SetInsertionPoint(0);
194 DECLARE_EVENT_TABLE()
197 //----------------------------------------------------------------------
199 //----------------------------------------------------------------------
203 //----------------------------------------------------------------------
205 //----------------------------------------------------------------------
216 TEXT_CLIPBOARD_COPY
= 200,
217 TEXT_CLIPBOARD_PASTE
,
220 TEXT_TOOLTIPS_SETDELAY
= 300,
221 TEXT_TOOLTIPS_ENABLE
,
238 // Create the main frame window
239 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
240 "Text wxWindows sample", 50, 50, 700, 420);
241 frame
->SetSizeHints( 500, 400 );
243 wxMenu
*file_menu
= new wxMenu
;
244 file_menu
->Append(TEXT_CLEAR
, "&Clear the log\tCtrl-C",
245 "Clear the log window contents");
246 file_menu
->Append(TEXT_SAVE
, "&Save file\tCtrl-S",
247 "Save the text control contents to file");
248 file_menu
->Append(TEXT_LOAD
, "&Load file\tCtrl-O",
249 "Load the sample file into text control");
250 file_menu
->AppendSeparator();
251 file_menu
->Append(TEXT_ABOUT
, "&About\tAlt-A");
252 file_menu
->AppendSeparator();
253 file_menu
->Append(TEXT_QUIT
, "E&xit\tAlt-X", "Quit this sample");
255 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
256 menu_bar
->Append(file_menu
, "&File");
259 wxMenu
*tooltip_menu
= new wxMenu
;
260 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, "Set &delay\tCtrl-D");
261 tooltip_menu
->AppendSeparator();
262 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, "&Toggle tooltips\tCtrl-T",
263 "enable/disable tooltips", TRUE
);
264 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, TRUE
);
265 menu_bar
->Append(tooltip_menu
, "&Tooltips");
266 #endif // wxUSE_TOOLTIPS
269 wxMenu
*menuClipboard
= new wxMenu
;
270 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, "&Copy\tCtrl-C",
271 "Copy the first line to the clipboard");
272 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, "&Paste\tCtrl-V",
273 "Paste from clipboard to the text control");
274 menu_bar
->Append(menuClipboard
, "&Clipboard");
275 #endif // wxUSE_CLIPBOARD
277 wxMenu
*menuText
= new wxMenu
;
278 menuText
->Append(TEXT_ADD_SOME
, "&Append some text\tCtrl-A");
279 menuText
->Append(TEXT_ADD_FREEZE
, "&Append text with freeze/thaw\tShift-Ctrl-A");
280 menuText
->Append(TEXT_MOVE_ENDTEXT
, "Move cursor to the end of &text");
281 menuText
->Append(TEXT_MOVE_ENDENTRY
, "Move cursor to the end of &entry");
282 menuText
->Append(TEXT_SET_EDITABLE
, "Toggle &editable state", "", TRUE
);
283 menuText
->Append(TEXT_SET_ENABLED
, "Toggle e&nabled state", "", TRUE
);
284 menuText
->Check(TEXT_SET_EDITABLE
, TRUE
);
285 menuText
->Check(TEXT_SET_ENABLED
, TRUE
);
286 menuText
->AppendSeparator();
287 menuText
->Append(TEXT_LINE_DOWN
, "Scroll text one line down");
288 menuText
->Append(TEXT_LINE_UP
, "Scroll text one line up");
289 menuText
->Append(TEXT_PAGE_DOWN
, "Scroll text one page down");
290 menuText
->Append(TEXT_PAGE_DOWN
, "Scroll text one page up");
291 menu_bar
->Append(menuText
, "&Text");
293 frame
->SetMenuBar(menu_bar
);
303 //----------------------------------------------------------------------
305 //----------------------------------------------------------------------
307 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
308 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
309 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
310 EVT_CHAR(MyTextCtrl::OnChar
)
311 EVT_TEXT(-1, MyTextCtrl::OnText
)
312 EVT_TEXT_URL(-1, MyTextCtrl::OnTextURL
)
313 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
316 void MyTextCtrl::LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const
319 long keycode
= event
.KeyCode();
323 case WXK_BACK
: key
= "BACK"; break;
324 case WXK_TAB
: key
= "TAB"; break;
325 case WXK_RETURN
: key
= "RETURN"; break;
326 case WXK_ESCAPE
: key
= "ESCAPE"; break;
327 case WXK_SPACE
: key
= "SPACE"; break;
328 case WXK_DELETE
: key
= "DELETE"; break;
329 case WXK_START
: key
= "START"; break;
330 case WXK_LBUTTON
: key
= "LBUTTON"; break;
331 case WXK_RBUTTON
: key
= "RBUTTON"; break;
332 case WXK_CANCEL
: key
= "CANCEL"; break;
333 case WXK_MBUTTON
: key
= "MBUTTON"; break;
334 case WXK_CLEAR
: key
= "CLEAR"; break;
335 case WXK_SHIFT
: key
= "SHIFT"; break;
336 case WXK_ALT
: key
= "ALT"; break;
337 case WXK_CONTROL
: key
= "CONTROL"; break;
338 case WXK_MENU
: key
= "MENU"; break;
339 case WXK_PAUSE
: key
= "PAUSE"; break;
340 case WXK_CAPITAL
: key
= "CAPITAL"; break;
341 case WXK_PRIOR
: key
= "PRIOR"; break;
342 case WXK_NEXT
: key
= "NEXT"; break;
343 case WXK_END
: key
= "END"; break;
344 case WXK_HOME
: key
= "HOME"; break;
345 case WXK_LEFT
: key
= "LEFT"; break;
346 case WXK_UP
: key
= "UP"; break;
347 case WXK_RIGHT
: key
= "RIGHT"; break;
348 case WXK_DOWN
: key
= "DOWN"; break;
349 case WXK_SELECT
: key
= "SELECT"; break;
350 case WXK_PRINT
: key
= "PRINT"; break;
351 case WXK_EXECUTE
: key
= "EXECUTE"; break;
352 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
353 case WXK_INSERT
: key
= "INSERT"; break;
354 case WXK_HELP
: key
= "HELP"; break;
355 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
356 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
357 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
358 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
359 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
360 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
361 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
362 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
363 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
364 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
365 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
366 case WXK_ADD
: key
= "ADD"; break;
367 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
368 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
369 case WXK_DECIMAL
: key
= "DECIMAL"; break;
370 case WXK_DIVIDE
: key
= "DIVIDE"; break;
371 case WXK_F1
: key
= "F1"; break;
372 case WXK_F2
: key
= "F2"; break;
373 case WXK_F3
: key
= "F3"; break;
374 case WXK_F4
: key
= "F4"; break;
375 case WXK_F5
: key
= "F5"; break;
376 case WXK_F6
: key
= "F6"; break;
377 case WXK_F7
: key
= "F7"; break;
378 case WXK_F8
: key
= "F8"; break;
379 case WXK_F9
: key
= "F9"; break;
380 case WXK_F10
: key
= "F10"; break;
381 case WXK_F11
: key
= "F11"; break;
382 case WXK_F12
: key
= "F12"; break;
383 case WXK_F13
: key
= "F13"; break;
384 case WXK_F14
: key
= "F14"; break;
385 case WXK_F15
: key
= "F15"; break;
386 case WXK_F16
: key
= "F16"; break;
387 case WXK_F17
: key
= "F17"; break;
388 case WXK_F18
: key
= "F18"; break;
389 case WXK_F19
: key
= "F19"; break;
390 case WXK_F20
: key
= "F20"; break;
391 case WXK_F21
: key
= "F21"; break;
392 case WXK_F22
: key
= "F22"; break;
393 case WXK_F23
: key
= "F23"; break;
394 case WXK_F24
: key
= "F24"; break;
395 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
396 case WXK_SCROLL
: key
= "SCROLL"; break;
397 case WXK_PAGEUP
: key
= "PAGEUP"; break;
398 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
399 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
400 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
401 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
402 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
403 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
404 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
405 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
406 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
407 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
408 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
409 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
410 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
411 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
412 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
413 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
414 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
415 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
416 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
417 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
418 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
419 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
420 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
421 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
422 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
423 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
427 if ( wxIsprint((int)keycode
) )
428 key
.Printf( _T("'%c'") , (char)keycode
);
430 key
.Printf( _T("unknown (%ld)"), keycode
);
435 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
438 GetChar( event
.ControlDown(), _T('C') ),
439 GetChar( event
.AltDown(), _T('A') ),
440 GetChar( event
.ShiftDown(), _T('S') ),
441 GetChar( event
.MetaDown(), _T('M') ) );
444 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
449 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
452 dbl
= ev
.LeftDClick();
455 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
457 button
= _T("Middle");
458 dbl
= ev
.MiddleDClick();
461 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
463 button
= _T("Right");
464 dbl
= ev
.RightDClick();
469 return _T("Unknown mouse event");
472 return wxString::Format(_T("%s mouse button %s"),
474 dbl
? _T("double clicked")
475 : up
? _T("released") : _T("clicked"));
478 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
485 msg
= _T("Mouse entered the window");
487 else if ( ev
.Leaving() )
489 msg
= _T("Mouse left the window");
493 msg
= GetMouseEventDesc(ev
);
496 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ")
498 << GetChar( ev
.LeftIsDown(), _T('1') )
499 << GetChar( ev
.MiddleIsDown(), _T('2') )
500 << GetChar( ev
.RightIsDown(), _T('3') )
501 << GetChar( ev
.ControlDown(), _T('C') )
502 << GetChar( ev
.AltDown(), _T('A') )
503 << GetChar( ev
.ShiftDown(), _T('S') )
504 << GetChar( ev
.MetaDown(), _T('M') );
508 //else: we're not interested in mouse move events
513 void MyTextCtrl::OnText(wxCommandEvent
& event
)
515 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
516 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
519 wxLogMessage(_T("Text changed in control '%s'"), data
);
523 wxLogMessage(_T("Text changed in some control"));
527 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
529 const wxMouseEvent
& ev
= event
.GetMouseEvent();
531 // filter out mouse moves, too many of them
535 long start
= event
.GetURLStart(),
536 end
= event
.GetURLEnd();
538 wxLogMessage(_T("Mouse event over URL '%s': %s"),
539 GetValue().Mid(start
, end
- start
).c_str(),
540 GetMouseEventDesc(ev
).c_str());
543 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
545 LogEvent( _T("Char"), event
);
547 /* How are we supposed to test wxTE_PROCESS_TAB with this code?
549 if ( event.KeyCode() == WXK_TAB )
561 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
563 LogEvent( _T("Key up"), event
);
568 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
570 switch ( event
.KeyCode() )
573 // show current position and text length
575 long line
, column
, pos
= GetInsertionPoint();
576 PositionToXY(pos
, &column
, &line
);
578 wxLogMessage( _T("Current position: %ld\n"
579 "Current line, column: (%ld, %ld)\n"
580 "Number of lines: %ld\n"
581 "Current line length: %ld\n"
582 "Total text length: %u (%ld)"),
594 SetInsertionPointEnd();
599 SetInsertionPoint(10);
605 wxLogDebug( wxT("Now capturing mouse and events.") );
611 wxLogDebug( wxT("Stopped capturing mouse and events.") );
618 // insert a blank line
623 SetValue("F6 was just pressed.");
631 LogEvent( wxT("Key down"), event
);
636 //----------------------------------------------------------------------
638 //----------------------------------------------------------------------
640 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
641 EVT_SIZE(MyPanel::OnSize
)
644 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
645 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
647 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE
);
649 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
653 // single line text controls
655 m_text
= new MyTextCtrl( this, -1, "Single line.",
656 wxPoint(10,10), wxSize(140,-1),
658 m_text
->SetForegroundColour(*wxBLUE
);
659 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
660 (*m_text
) << " Appended.";
661 m_text
->SetInsertionPoint(0);
662 m_text
->WriteText( "Prepended. " );
664 m_password
= new MyTextCtrl( this, -1, "",
665 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
667 m_readonly
= new MyTextCtrl( this, -1, "Read only",
668 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
670 // multi line text controls
672 m_horizontal
= new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
673 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
675 // a little hack to use the command line argument for encoding testing
676 if ( wxTheApp
->argc
== 2 )
678 switch ( wxTheApp
->argv
[1][0] )
681 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
683 wxFONTENCODING_ISO8859_2
));
684 m_horizontal
->SetValue("®lu»ouèký kùò zbìsile èe¹tina «»");
688 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
690 wxFONTENCODING_KOI8
));
691 m_horizontal
->SetValue("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
696 m_horizontal
->SetValue("Text in default encoding");
699 m_multitext
= new MyTextCtrl( this, -1, "Multi line.",
700 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
701 m_multitext
->SetFont(*wxITALIC_FONT
);
702 (*m_multitext
) << " Appended.";
703 m_multitext
->SetInsertionPoint(0);
704 m_multitext
->WriteText( "Prepended. " );
705 m_multitext
->SetForegroundColour(*wxRED
);
706 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
709 m_multitext
->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
712 m_tab
= new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
713 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
714 m_tab
->SetClientData((void *)_T("tab"));
716 m_enter
= new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
717 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
718 m_enter
->SetClientData((void *)_T("enter"));
720 m_textrich
= new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
721 "(even under broken Win9x)\n"
722 "and a very very very very very "
723 "very very very long line to test "
725 wxPoint(450, 10), wxSize(230, 230),
732 m_textrich
->SetFont(wxFont(12, wxFONTFAMILY_TELETYPE
,
733 wxFONTSTYLE_NORMAL
, wxFONTWEIGHT_NORMAL
));
734 m_textrich
->SetStyle(0, 10, *wxRED
);
735 m_textrich
->SetStyle(10, 20, *wxBLUE
);
736 m_textrich
->SetStyle(30, 40,
737 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
738 m_textrich
->SetDefaultStyle(wxTextAttr());
739 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
740 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
741 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
742 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
743 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
744 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
745 m_textrich
->SetDefaultStyle(*wxBLUE
);
746 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
747 _T("type should be in blue as well"));
749 m_textrich
->SetDefaultStyle(wxTextAttr(*wxRED
));
750 m_textrich
->AppendText(_T("Red text\n"));
751 m_textrich
->SetDefaultStyle(wxTextAttr(wxNullColour
, *wxLIGHT_GREY
));
752 m_textrich
->AppendText(_T("Red on grey text\n"));
753 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
));
754 m_textrich
->AppendText(_T("Blue on grey text\n"));
758 void MyPanel::OnSize( wxSizeEvent
&event
)
760 wxSize
client_area( GetClientSize() );
761 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
766 void MyPanel::DoPasteFromClipboard()
768 // On X11, we want to get the data from the primary selection instead
769 // of the normal clipboard (which isn't normal under X11 at all). This
770 // call has no effect under MSW.
771 wxTheClipboard
->UsePrimarySelection();
773 if (!wxTheClipboard
->Open())
775 *m_log
<< "Error opening the clipboard.\n";
780 *m_log
<< "Successfully opened the clipboard.\n";
783 wxTextDataObject data
;
785 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
787 *m_log
<< "Clipboard supports requested format.\n";
789 if (wxTheClipboard
->GetData( data
))
791 *m_log
<< "Successfully retrieved data from the clipboard.\n";
792 *m_multitext
<< data
.GetText() << "\n";
796 *m_log
<< "Error getting data from the clipboard.\n";
801 *m_log
<< "Clipboard doesn't support requested format.\n";
804 wxTheClipboard
->Close();
806 *m_log
<< "Closed the clipboard.\n";
809 void MyPanel::DoCopyToClipboard()
811 // On X11, we want to get the data from the primary selection instead
812 // of the normal clipboard (which isn't normal under X11 at all). This
813 // call has no effect under MSW.
814 wxTheClipboard
->UsePrimarySelection();
816 wxString
text( m_multitext
->GetLineText(0) );
820 *m_log
<< "No text to copy.\n";
825 if (!wxTheClipboard
->Open())
827 *m_log
<< "Error opening the clipboard.\n";
833 *m_log
<< "Successfully opened the clipboard.\n";
836 wxTextDataObject
*data
= new wxTextDataObject( text
);
838 if (!wxTheClipboard
->SetData( data
))
840 *m_log
<< "Error while copying to the clipboard.\n";
844 *m_log
<< "Successfully copied data to the clipboard.\n";
847 wxTheClipboard
->Close();
849 *m_log
<< "Closed the clipboard.\n";
852 #endif // wxUSE_CLIPBOARD
854 void MyPanel::DoMoveToEndOfText()
856 m_multitext
->SetInsertionPointEnd();
857 m_multitext
->SetFocus();
860 void MyPanel::DoMoveToEndOfEntry()
862 m_text
->SetInsertionPointEnd();
866 //----------------------------------------------------------------------
868 //----------------------------------------------------------------------
870 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
871 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
872 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
873 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
874 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
875 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
878 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
879 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
880 #endif // wxUSE_TOOLTIPS
883 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
884 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
885 #endif // wxUSE_CLIPBOARD
887 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
888 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
889 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
890 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
892 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
893 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
895 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
896 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
897 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
898 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
900 EVT_IDLE(MyFrame::OnIdle
)
903 MyFrame::MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
)
904 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
) )
908 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
911 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
916 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
920 wxMessageDialog
dialog(this,
921 "This is a text control sample. It demonstrates the many different\n"
922 "text control styles, the use of the clipboard, setting and handling\n"
923 "tooltips and intercepting key and char events.\n"
925 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
926 "About wxTextCtrl Sample",
927 wxOK
| wxICON_INFORMATION
);
935 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
937 static long s_delay
= 5000;
940 delay
.Printf( _T("%ld"), s_delay
);
942 delay
= wxGetTextFromUser("Enter delay (in milliseconds)",
949 wxSscanf(delay
, _T("%ld"), &s_delay
);
951 wxToolTip::SetDelay(s_delay
);
953 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
956 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
958 static bool s_enabled
= TRUE
;
960 s_enabled
= !s_enabled
;
962 wxToolTip::Enable(s_enabled
);
964 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
968 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
970 m_panel
->m_log
->Clear();
973 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
975 static bool s_editable
= TRUE
;
977 s_editable
= !s_editable
;
978 m_panel
->m_text
->SetEditable(s_editable
);
979 m_panel
->m_password
->SetEditable(s_editable
);
980 m_panel
->m_multitext
->SetEditable(s_editable
);
981 m_panel
->m_textrich
->SetEditable(s_editable
);
984 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
986 bool enabled
= m_panel
->m_text
->IsEnabled();
989 m_panel
->m_text
->Enable(enabled
);
990 m_panel
->m_password
->Enable(enabled
);
991 m_panel
->m_multitext
->Enable(enabled
);
992 m_panel
->m_readonly
->Enable(enabled
);
993 m_panel
->m_textrich
->Enable(enabled
);
996 void MyFrame::OnFileSave(wxCommandEvent
& event
)
998 if ( m_panel
->m_textrich
->SaveFile("dummy.txt") )
1001 // verify that the fil length is correct (it wasn't under Win95)
1002 wxFile
file("dummy.txt");
1003 wxLogStatus(this, _T("Successfully saved file "
1004 "(text len = %ld, file size = %ld)"),
1005 m_panel
->m_textrich
->GetValue().length(),
1010 wxLogStatus(this, _T("Couldn't save the file"));
1013 void MyFrame::OnFileLoad(wxCommandEvent
& event
)
1015 if ( m_panel
->m_textrich
->LoadFile("dummy.txt") )
1016 wxLogStatus(this, _T("Successfully loaded file"));
1018 wxLogStatus(this, _T("Couldn't load the file"));
1021 void MyFrame::OnIdle( wxIdleEvent
& event
)
1023 // track the window which has the focus in the status bar
1024 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1025 wxWindow
*focus
= wxWindow::FindFocus();
1026 if ( focus
&& (focus
!= s_windowFocus
) )
1028 s_windowFocus
= focus
;
1033 _T("Focus: wxWindow = %p, HWND = %08x"),
1035 _T("Focus: wxWindow = %p"),
1039 , s_windowFocus
->GetHWND()