1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: TextCtrl wxWidgets sample
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
22 #include "wx/dataobj.h"
23 #include "wx/clipbrd.h"
31 #include "wx/tooltip.h"
34 // We test for wxUSE_DRAG_AND_DROP also, because data objects may not be
35 // implemented for compilers that can't cope with the OLE parts in
36 // wxUSE_DRAG_AND_DROP.
37 #if !wxUSE_DRAG_AND_DROP
38 #undef wxUSE_CLIPBOARD
39 #define wxUSE_CLIPBOARD 0
42 #include "wx/colordlg.h"
43 #include "wx/fontdlg.h"
44 #include "wx/numdlg.h"
45 #include "wx/tokenzr.h"
47 #ifndef wxHAS_IMAGES_IN_RESOURCES
48 #include "../sample.xpm"
51 //----------------------------------------------------------------------
53 //----------------------------------------------------------------------
55 class MyApp
: public wxApp
61 // a text ctrl which allows to call different wxTextCtrl functions
62 // interactively by pressing function keys in it
63 class MyTextCtrl
: public wxTextCtrl
66 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
67 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
68 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
)
73 void OnKeyDown(wxKeyEvent
& event
);
74 void OnKeyUp(wxKeyEvent
& event
);
75 void OnChar(wxKeyEvent
& event
);
77 void OnText(wxCommandEvent
& event
);
78 void OnTextEnter(wxCommandEvent
& event
);
79 void OnTextURL(wxTextUrlEvent
& event
);
80 void OnTextMaxLen(wxCommandEvent
& event
);
82 void OnTextCut(wxClipboardTextEvent
& event
);
83 void OnTextCopy(wxClipboardTextEvent
& event
);
84 void OnTextPaste(wxClipboardTextEvent
& event
);
86 void OnMouseEvent(wxMouseEvent
& event
);
88 void OnSetFocus(wxFocusEvent
& event
);
89 void OnKillFocus(wxFocusEvent
& event
);
91 static bool ms_logKey
;
92 static bool ms_logChar
;
93 static bool ms_logMouse
;
94 static bool ms_logText
;
95 static bool ms_logFocus
;
96 static bool ms_logClip
;
99 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: wxT('-'); }
101 void LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
102 void LogClipEvent(const wxChar
*what
, wxClipboardTextEvent
& event
);
106 DECLARE_EVENT_TABLE()
109 class MyPanel
: public wxPanel
112 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
116 delete wxLog::SetActiveTarget(m_logOld
);
121 void DoPasteFromClipboard();
122 void DoCopyToClipboard();
123 #endif // wxUSE_CLIPBOARD
126 void DoReplaceText();
128 void DoMoveToEndOfText();
129 void DoMoveToEndOfEntry();
130 void DoGetWindowCoordinates();
132 // return true if currently text control has any selection
133 bool HasSelection() const
136 GetFocusedText()->GetSelection(&from
, &to
);
141 MyTextCtrl
*m_password
;
144 MyTextCtrl
*m_readonly
;
145 MyTextCtrl
*m_limited
;
147 MyTextCtrl
*m_multitext
;
148 MyTextCtrl
*m_horizontal
;
150 MyTextCtrl
*m_textrich
;
158 // get the currently focused text control or return the default one
159 // (m_multitext) is no text ctrl has focus -- in any case, returns
160 // something non NULL
161 wxTextCtrl
*GetFocusedText() const;
164 class MyFrame
: public wxFrame
167 MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
169 void OnQuit(wxCommandEvent
& event
);
170 void OnAbout(wxCommandEvent
& event
);
172 void OnSetTooltipDelay(wxCommandEvent
& event
);
173 void OnToggleTooltips(wxCommandEvent
& event
);
174 #endif // wxUSE_TOOLTIPS
177 void OnPasteFromClipboard( wxCommandEvent
& WXUNUSED(event
) )
179 wxLogMessage(wxT("Pasting text from clipboard."));
180 m_panel
->DoPasteFromClipboard();
182 void OnCopyToClipboard( wxCommandEvent
& WXUNUSED(event
) )
184 wxLogMessage(wxT("Copying text to clipboard."));
185 m_panel
->DoCopyToClipboard();
188 void OnUpdatePasteFromClipboard(wxUpdateUIEvent
& event
)
190 wxClipboardLocker lockClip
;
192 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
195 void OnUpdateCopyToClipboard(wxUpdateUIEvent
& event
)
197 event
.Enable( m_panel
->HasSelection() );
199 #endif // wxUSE_CLIPBOARD
201 void OnAddTextLine(wxCommandEvent
& WXUNUSED(event
))
204 m_panel
->m_textrich
->AppendText(wxString::Format("Line %d\n", ++s_n
));
207 void OnAddTextFreeze( wxCommandEvent
& WXUNUSED(event
) )
209 void OnAddText( wxCommandEvent
& WXUNUSED(event
) )
210 { DoAddText(false); }
211 void OnRemoveText( wxCommandEvent
& WXUNUSED(event
) )
212 { m_panel
->DoRemoveText(); }
213 void OnReplaceText( wxCommandEvent
& WXUNUSED(event
) )
214 { m_panel
->DoReplaceText(); }
215 void OnSelectText( wxCommandEvent
& WXUNUSED(event
) )
216 { m_panel
->DoSelectText(); }
218 void OnMoveToEndOfText( wxCommandEvent
& WXUNUSED(event
) )
219 { m_panel
->DoMoveToEndOfText(); }
221 void OnGetWindowCoordinates( wxCommandEvent
& WXUNUSED(event
) )
222 { m_panel
->DoGetWindowCoordinates(); }
224 void OnMoveToEndOfEntry( wxCommandEvent
& WXUNUSED(event
) )
225 { m_panel
->DoMoveToEndOfEntry(); }
227 void OnScrollLineDown(wxCommandEvent
& WXUNUSED(event
))
229 if ( !m_panel
->m_textrich
->LineDown() )
231 wxLogMessage(wxT("Already at the bottom"));
235 void OnScrollLineUp(wxCommandEvent
& WXUNUSED(event
))
237 if ( !m_panel
->m_textrich
->LineUp() )
239 wxLogMessage(wxT("Already at the top"));
243 void OnScrollPageDown(wxCommandEvent
& WXUNUSED(event
))
245 if ( !m_panel
->m_textrich
->PageDown() )
247 wxLogMessage(wxT("Already at the bottom"));
251 void OnScrollPageUp(wxCommandEvent
& WXUNUSED(event
))
253 if ( !m_panel
->m_textrich
->PageUp() )
255 wxLogMessage(wxT("Already at the top"));
259 void OnGetLine(wxCommandEvent
& WXUNUSED(event
))
261 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
262 wxT("Enter which line you would like to get"),
263 wxT("Get a line from the tabbed multiline text control") );
265 wxMessageBox(m_panel
->m_tab
->GetLineText(nLine
));
268 void OnGetLineLength(wxCommandEvent
& WXUNUSED(event
))
270 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
271 wxT("Enter which line you would like to get"),
272 wxT("Get length of a line from the tabbed multiline text control") );
274 wxMessageBox(wxString::Format(wxT("Length of line %i is:%i"),
276 m_panel
->m_tab
->GetLineLength(nLine
))
281 void OnLogClear(wxCommandEvent
& event
);
283 void OnFileSave(wxCommandEvent
& event
);
284 void OnFileLoad(wxCommandEvent
& event
);
285 void OnRichTextTest(wxCommandEvent
& event
);
287 void OnSetEditable(wxCommandEvent
& event
);
288 void OnSetEnabled(wxCommandEvent
& event
);
290 void OnLogKey(wxCommandEvent
& event
)
292 MyTextCtrl::ms_logKey
= event
.IsChecked();
295 void OnLogChar(wxCommandEvent
& event
)
297 MyTextCtrl::ms_logChar
= event
.IsChecked();
300 void OnLogMouse(wxCommandEvent
& event
)
302 MyTextCtrl::ms_logMouse
= event
.IsChecked();
305 void OnLogText(wxCommandEvent
& event
)
307 MyTextCtrl::ms_logText
= event
.IsChecked();
310 void OnLogFocus(wxCommandEvent
& event
)
312 MyTextCtrl::ms_logFocus
= event
.IsChecked();
315 void OnLogClip(wxCommandEvent
& event
)
317 MyTextCtrl::ms_logClip
= event
.IsChecked();
320 void OnSetText(wxCommandEvent
& WXUNUSED(event
))
322 m_panel
->m_text
->SetValue(wxT("Hello, world! (what else did you expect?)"));
325 void OnChangeText(wxCommandEvent
& WXUNUSED(event
))
327 m_panel
->m_text
->ChangeValue(wxT("Changed, not set: no event"));
330 void OnIdle( wxIdleEvent
& event
);
333 void DoAddText(bool freeze
)
335 wxTextCtrl
* const text
= m_panel
->m_textrich
;
342 for ( int i
= 0; i
< 100; i
++ )
344 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
347 text
->SetInsertionPoint(0);
355 DECLARE_EVENT_TABLE()
359 * RichTextFrame is used to demonstrate rich text behaviour
362 class RichTextFrame
: public wxFrame
365 RichTextFrame(wxWindow
* parent
, const wxString
& title
);
369 void OnClose(wxCommandEvent
& event
);
370 void OnIdle(wxIdleEvent
& event
);
371 void OnLeftAlign(wxCommandEvent
& event
);
372 void OnRightAlign(wxCommandEvent
& event
);
373 void OnJustify(wxCommandEvent
& event
);
374 void OnCentre(wxCommandEvent
& event
);
375 void OnChangeFont(wxCommandEvent
& event
);
376 void OnChangeTextColour(wxCommandEvent
& event
);
377 void OnChangeBackgroundColour(wxCommandEvent
& event
);
378 void OnLeftIndent(wxCommandEvent
& event
);
379 void OnRightIndent(wxCommandEvent
& event
);
380 void OnTabStops(wxCommandEvent
& event
);
383 wxTextCtrl
*m_textCtrl
;
384 long m_currentPosition
;
386 DECLARE_EVENT_TABLE()
389 //----------------------------------------------------------------------
391 //----------------------------------------------------------------------
395 //----------------------------------------------------------------------
397 //----------------------------------------------------------------------
401 TEXT_QUIT
= wxID_EXIT
,
402 TEXT_ABOUT
= wxID_ABOUT
,
409 TEXT_CLIPBOARD_COPY
= 200,
410 TEXT_CLIPBOARD_PASTE
,
414 TEXT_TOOLTIPS_SETDELAY
= 300,
415 TEXT_TOOLTIPS_ENABLE
,
422 TEXT_GET_WINDOW_COORD
,
453 if ( !wxApp::OnInit() )
456 // Create the main frame window
457 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
458 wxT("Text wxWidgets sample"), 50, 50, 700, 550);
459 frame
->SetSizeHints( 500, 400 );
461 wxMenu
*file_menu
= new wxMenu
;
462 file_menu
->Append(TEXT_SAVE
, wxT("&Save file\tCtrl-S"),
463 wxT("Save the text control contents to file"));
464 file_menu
->Append(TEXT_LOAD
, wxT("&Load file\tCtrl-O"),
465 wxT("Load the sample file into text control"));
466 file_menu
->AppendSeparator();
467 file_menu
->Append(TEXT_RICH_TEXT_TEST
, wxT("Show Rich Text Editor"));
468 file_menu
->AppendSeparator();
469 file_menu
->Append(TEXT_ABOUT
, wxT("&About\tAlt-A"));
470 file_menu
->AppendSeparator();
471 file_menu
->Append(TEXT_QUIT
, wxT("E&xit\tAlt-X"), wxT("Quit this sample"));
473 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
474 menu_bar
->Append(file_menu
, wxT("&File"));
477 wxMenu
*tooltip_menu
= new wxMenu
;
478 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, wxT("Set &delay\tCtrl-D"));
479 tooltip_menu
->AppendSeparator();
480 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, wxT("&Toggle tooltips\tCtrl-T"),
481 wxT("enable/disable tooltips"), true);
482 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, true);
483 menu_bar
->Append(tooltip_menu
, wxT("&Tooltips"));
484 #endif // wxUSE_TOOLTIPS
487 // notice that we use non default accelerators on purpose here to compare
488 // their behaviour with the built in handling of standard Ctrl/Cmd-C/V
489 wxMenu
*menuClipboard
= new wxMenu
;
490 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, wxT("&Copy\tCtrl-Shift-C"),
491 wxT("Copy the selection to the clipboard"));
492 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, wxT("&Paste\tCtrl-Shift-V"),
493 wxT("Paste from clipboard to the text control"));
494 menuClipboard
->AppendSeparator();
495 menuClipboard
->AppendCheckItem(TEXT_CLIPBOARD_VETO
, wxT("Vet&o\tCtrl-Shift-O"),
496 wxT("Veto all clipboard operations"));
497 menu_bar
->Append(menuClipboard
, wxT("&Clipboard"));
498 #endif // wxUSE_CLIPBOARD
500 wxMenu
*menuText
= new wxMenu
;
501 menuText
->Append(TEXT_ADD_SOME
, wxT("&Append some text\tCtrl-A"));
502 menuText
->Append(TEXT_ADD_FREEZE
, wxT("&Append text with freeze/thaw\tShift-Ctrl-A"));
503 menuText
->Append(TEXT_ADD_LINE
, wxT("Append a new &line\tAlt-Shift-A"));
504 menuText
->Append(TEXT_REMOVE
, wxT("&Remove first 10 characters\tCtrl-Y"));
505 menuText
->Append(TEXT_REPLACE
, wxT("&Replace characters 4 to 8 with ABC\tCtrl-R"));
506 menuText
->Append(TEXT_SELECT
, wxT("&Select characters 4 to 8\tCtrl-I"));
507 menuText
->Append(TEXT_SET
, wxT("&Set the first text zone value\tCtrl-E"));
508 menuText
->Append(TEXT_CHANGE
, wxT("&Change the first text zone value\tShift-Ctrl-E"));
509 menuText
->AppendSeparator();
510 menuText
->Append(TEXT_MOVE_ENDTEXT
, wxT("Move cursor to the end of &text"));
511 menuText
->Append(TEXT_MOVE_ENDENTRY
, wxT("Move cursor to the end of &entry"));
512 menuText
->AppendCheckItem(TEXT_SET_EDITABLE
, wxT("Toggle &editable state"));
513 menuText
->AppendCheckItem(TEXT_SET_ENABLED
, wxT("Toggle e&nabled state"));
514 menuText
->Check(TEXT_SET_EDITABLE
, true);
515 menuText
->Check(TEXT_SET_ENABLED
, true);
516 menuText
->AppendSeparator();
517 menuText
->Append(TEXT_LINE_DOWN
, wxT("Scroll text one line down"));
518 menuText
->Append(TEXT_LINE_UP
, wxT("Scroll text one line up"));
519 menuText
->Append(TEXT_PAGE_DOWN
, wxT("Scroll text one page down"));
520 menuText
->Append(TEXT_PAGE_UP
, wxT("Scroll text one page up"));
521 menuText
->Append(TEXT_GET_WINDOW_COORD
, wxT("Get window coordinates"));
522 menuText
->AppendSeparator();
523 menuText
->Append(TEXT_GET_LINE
, wxT("Get the text of a line of the tabbed multiline"));
524 menuText
->Append(TEXT_GET_LINELENGTH
, wxT("Get the length of a line of the tabbed multiline"));
525 menu_bar
->Append(menuText
, wxT("Te&xt"));
528 wxMenu
*menuLog
= new wxMenu
;
529 menuLog
->AppendCheckItem(TEXT_LOG_KEY
, wxT("Log &key events"));
530 menuLog
->AppendCheckItem(TEXT_LOG_CHAR
, wxT("Log &char events"));
531 menuLog
->AppendCheckItem(TEXT_LOG_MOUSE
, wxT("Log &mouse events"));
532 menuLog
->AppendCheckItem(TEXT_LOG_TEXT
, wxT("Log &text events"));
533 menuLog
->AppendCheckItem(TEXT_LOG_FOCUS
, wxT("Log &focus events"));
534 menuLog
->AppendCheckItem(TEXT_LOG_CLIP
, wxT("Log clip&board events"));
535 menuLog
->AppendSeparator();
536 menuLog
->Append(TEXT_CLEAR
, wxT("&Clear the log\tCtrl-L"),
537 wxT("Clear the log window contents"));
539 // select only the interesting events by default
540 MyTextCtrl::ms_logClip
=
541 MyTextCtrl::ms_logText
= true;
543 menuLog
->Check(TEXT_LOG_KEY
, MyTextCtrl::ms_logKey
);
544 menuLog
->Check(TEXT_LOG_CHAR
, MyTextCtrl::ms_logChar
);
545 menuLog
->Check(TEXT_LOG_TEXT
, MyTextCtrl::ms_logText
);
547 menu_bar
->Append(menuLog
, wxT("&Log"));
550 frame
->SetMenuBar(menu_bar
);
558 //----------------------------------------------------------------------
560 //----------------------------------------------------------------------
562 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
563 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
564 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
565 EVT_CHAR(MyTextCtrl::OnChar
)
567 EVT_TEXT(wxID_ANY
, MyTextCtrl::OnText
)
568 EVT_TEXT_ENTER(wxID_ANY
, MyTextCtrl::OnTextEnter
)
569 EVT_TEXT_URL(wxID_ANY
, MyTextCtrl::OnTextURL
)
570 EVT_TEXT_MAXLEN(wxID_ANY
, MyTextCtrl::OnTextMaxLen
)
571 EVT_TEXT_CUT(wxID_ANY
, MyTextCtrl::OnTextCut
)
572 EVT_TEXT_COPY(wxID_ANY
, MyTextCtrl::OnTextCopy
)
573 EVT_TEXT_PASTE(wxID_ANY
, MyTextCtrl::OnTextPaste
)
575 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
577 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus
)
578 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus
)
581 bool MyTextCtrl::ms_logKey
= false;
582 bool MyTextCtrl::ms_logChar
= false;
583 bool MyTextCtrl::ms_logMouse
= false;
584 bool MyTextCtrl::ms_logText
= false;
585 bool MyTextCtrl::ms_logFocus
= false;
586 bool MyTextCtrl::ms_logClip
= false;
588 void MyTextCtrl::LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const
591 long keycode
= event
.GetKeyCode();
595 case WXK_BACK
: key
= wxT("BACK"); break;
596 case WXK_TAB
: key
= wxT("TAB"); break;
597 case WXK_RETURN
: key
= wxT("RETURN"); break;
598 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
599 case WXK_SPACE
: key
= wxT("SPACE"); break;
600 case WXK_DELETE
: key
= wxT("DELETE"); break;
601 case WXK_START
: key
= wxT("START"); break;
602 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
603 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
604 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
605 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
606 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
607 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
608 case WXK_ALT
: key
= wxT("ALT"); break;
609 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
610 case WXK_MENU
: key
= wxT("MENU"); break;
611 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
612 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
613 case WXK_END
: key
= wxT("END"); break;
614 case WXK_HOME
: key
= wxT("HOME"); break;
615 case WXK_LEFT
: key
= wxT("LEFT"); break;
616 case WXK_UP
: key
= wxT("UP"); break;
617 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
618 case WXK_DOWN
: key
= wxT("DOWN"); break;
619 case WXK_SELECT
: key
= wxT("SELECT"); break;
620 case WXK_PRINT
: key
= wxT("PRINT"); break;
621 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
622 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
623 case WXK_INSERT
: key
= wxT("INSERT"); break;
624 case WXK_HELP
: key
= wxT("HELP"); break;
625 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
626 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
627 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
628 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
629 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
630 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
631 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
632 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
633 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
634 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
635 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
636 case WXK_ADD
: key
= wxT("ADD"); break;
637 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
638 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
639 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
640 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
641 case WXK_F1
: key
= wxT("F1"); break;
642 case WXK_F2
: key
= wxT("F2"); break;
643 case WXK_F3
: key
= wxT("F3"); break;
644 case WXK_F4
: key
= wxT("F4"); break;
645 case WXK_F5
: key
= wxT("F5"); break;
646 case WXK_F6
: key
= wxT("F6"); break;
647 case WXK_F7
: key
= wxT("F7"); break;
648 case WXK_F8
: key
= wxT("F8"); break;
649 case WXK_F9
: key
= wxT("F9"); break;
650 case WXK_F10
: key
= wxT("F10"); break;
651 case WXK_F11
: key
= wxT("F11"); break;
652 case WXK_F12
: key
= wxT("F12"); break;
653 case WXK_F13
: key
= wxT("F13"); break;
654 case WXK_F14
: key
= wxT("F14"); break;
655 case WXK_F15
: key
= wxT("F15"); break;
656 case WXK_F16
: key
= wxT("F16"); break;
657 case WXK_F17
: key
= wxT("F17"); break;
658 case WXK_F18
: key
= wxT("F18"); break;
659 case WXK_F19
: key
= wxT("F19"); break;
660 case WXK_F20
: key
= wxT("F20"); break;
661 case WXK_F21
: key
= wxT("F21"); break;
662 case WXK_F22
: key
= wxT("F22"); break;
663 case WXK_F23
: key
= wxT("F23"); break;
664 case WXK_F24
: key
= wxT("F24"); break;
665 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
666 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
667 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
668 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
669 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
670 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
671 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
672 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
673 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
674 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
675 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
676 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
677 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
678 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
679 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
680 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
681 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
682 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
683 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
684 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
685 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
686 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
687 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
688 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
689 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
690 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
691 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
692 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
696 if ( wxIsprint((int)keycode
) )
697 key
.Printf(wxT("'%c'"), (char)keycode
);
698 else if ( keycode
> 0 && keycode
< 27 )
699 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
701 key
.Printf(wxT("unknown (%ld)"), keycode
);
707 key
+= wxString::Format(wxT(" (Unicode: %#04x)"), event
.GetUnicodeKey());
708 #endif // wxUSE_UNICODE
710 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
713 GetChar( event
.ControlDown(), wxT('C') ),
714 GetChar( event
.AltDown(), wxT('A') ),
715 GetChar( event
.ShiftDown(), wxT('S') ),
716 GetChar( event
.MetaDown(), wxT('M') ) );
719 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
724 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
726 button
= wxT("Left");
727 dbl
= ev
.LeftDClick();
730 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
732 button
= wxT("Middle");
733 dbl
= ev
.MiddleDClick();
736 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
738 button
= wxT("Right");
739 dbl
= ev
.RightDClick();
742 else if ( ev
.Aux1Down() || ev
.Aux1Up() || ev
.Aux1DClick() )
744 button
= wxT("Aux1");
745 dbl
= ev
.Aux1DClick();
748 else if ( ev
.Aux2Down() || ev
.Aux2Up() || ev
.Aux2DClick() )
750 button
= wxT("Aux2");
751 dbl
= ev
.Aux2DClick();
754 else if ( ev
.GetWheelRotation() )
756 return wxString::Format("Wheel rotation %+d", ev
.GetWheelRotation());
760 return wxT("Unknown mouse event");
762 wxASSERT(!(dbl
&& up
));
764 return wxString::Format(wxT("%s mouse button %s"),
766 dbl
? wxT("double clicked")
767 : up
? wxT("released") : wxT("clicked"));
770 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
782 msg
= wxT("Mouse entered the window");
784 else if ( ev
.Leaving() )
786 msg
= wxT("Mouse left the window");
790 msg
= GetMouseEventDesc(ev
);
793 msg
<< wxT(" at (") << ev
.GetX() << wxT(", ") << ev
.GetY() << wxT(") ");
796 wxTextCtrlHitTestResult rc
= HitTest(ev
.GetPosition(), &pos
);
797 if ( rc
!= wxTE_HT_UNKNOWN
)
799 msg
<< wxT("at position ") << pos
<< wxT(' ');
802 msg
<< wxT("[Flags: ")
803 << GetChar( ev
.LeftIsDown(), wxT('1') )
804 << GetChar( ev
.MiddleIsDown(), wxT('2') )
805 << GetChar( ev
.RightIsDown(), wxT('3') )
806 << GetChar( ev
.Aux1IsDown(), wxT('x') )
807 << GetChar( ev
.Aux2IsDown(), wxT('X') )
808 << GetChar( ev
.ControlDown(), wxT('C') )
809 << GetChar( ev
.AltDown(), wxT('A') )
810 << GetChar( ev
.ShiftDown(), wxT('S') )
811 << GetChar( ev
.MetaDown(), wxT('M') )
816 //else: we're not interested in mouse move events
819 void MyTextCtrl::OnSetFocus(wxFocusEvent
& event
)
823 wxLogMessage( wxT("%p got focus."), this);
829 void MyTextCtrl::OnKillFocus(wxFocusEvent
& event
)
833 wxLogMessage( wxT("%p lost focus"), this);
839 void MyTextCtrl::OnText(wxCommandEvent
& event
)
844 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
845 const wxChar
*changeVerb
= win
->IsModified() ? wxT("changed")
846 : wxT("set by program");
847 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
850 wxLogMessage(wxT("Text %s in control \"%s\""), changeVerb
, data
);
854 wxLogMessage(wxT("Text %s in some control"), changeVerb
);
858 void MyTextCtrl::OnTextEnter(wxCommandEvent
& event
)
863 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
864 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
867 wxLogMessage(wxT("Enter pressed in control '%s'"), data
);
871 wxLogMessage(wxT("Enter pressed in some control"));
875 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& WXUNUSED(event
))
877 wxLogMessage(wxT("You can't enter more characters into this control."));
881 void MyTextCtrl::OnTextCut(wxClipboardTextEvent
& event
)
883 LogClipEvent(wxT("cut to"), event
);
886 void MyTextCtrl::OnTextCopy(wxClipboardTextEvent
& event
)
888 LogClipEvent(wxT("copied to"), event
);
891 void MyTextCtrl::OnTextPaste(wxClipboardTextEvent
& event
)
893 LogClipEvent(wxT("pasted from"), event
);
896 void MyTextCtrl::LogClipEvent(const wxChar
*what
, wxClipboardTextEvent
& event
)
898 wxFrame
*frame
= wxDynamicCast(wxGetTopLevelParent(this), wxFrame
);
899 wxCHECK_RET( frame
, wxT("no parent frame?") );
901 const bool veto
= frame
->GetMenuBar()->IsChecked(TEXT_CLIPBOARD_VETO
);
907 wxLogMessage(wxT("Text %s%s the clipboard."),
908 veto
? wxT("not ") : wxT(""), what
);
913 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
915 const wxMouseEvent
& ev
= event
.GetMouseEvent();
917 // filter out mouse moves, too many of them
921 long start
= event
.GetURLStart(),
922 end
= event
.GetURLEnd();
924 wxLogMessage(wxT("Mouse event over URL '%s': %s"),
925 GetValue().Mid(start
, end
- start
).c_str(),
926 GetMouseEventDesc(ev
).c_str());
929 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
932 LogKeyEvent( wxT("Char"), event
);
937 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
940 LogKeyEvent( wxT("Key up"), event
);
945 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
947 switch ( event
.GetKeyCode() )
950 // show current position and text length
952 long line
, column
, pos
= GetInsertionPoint();
953 PositionToXY(pos
, &column
, &line
);
955 wxLogMessage(wxT("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
958 (long) GetNumberOfLines(),
959 (long) GetLineLength(line
),
960 (unsigned int) GetValue().length(),
964 GetSelection(&from
, &to
);
966 wxString sel
= GetStringSelection();
968 wxLogMessage(wxT("Selection: from %ld to %ld."), from
, to
);
969 wxLogMessage(wxT("Selection = '%s' (len = %u)"),
971 (unsigned int) sel
.length());
973 const wxString text
= GetLineText(line
);
974 wxLogMessage(wxT("Current line: \"%s\"; length = %lu"),
975 text
.c_str(), text
.length());
981 SetInsertionPointEnd();
986 SetInsertionPoint(10);
992 wxLogDebug( wxT("Now capturing mouse and events.") );
998 wxLogDebug( wxT("Stopped capturing mouse and events.") );
999 m_hasCapture
= false;
1005 // insert a blank line
1006 WriteText(wxT("\n"));
1010 wxLogMessage(wxT("IsModified() before SetValue(): %d"),
1012 ChangeValue(wxT("ChangeValue() has been called"));
1013 wxLogMessage(wxT("IsModified() after SetValue(): %d"),
1018 wxLogMessage(wxT("Position 10 should be now visible."));
1023 wxLogMessage(wxT("Control has been cleared"));
1028 WriteText(wxT("WriteText() has been called"));
1032 AppendText(wxT("AppendText() has been called"));
1037 wxLogMessage(wxT("Control marked as non modified"));
1042 LogKeyEvent( wxT("Key down"), event
);
1047 //----------------------------------------------------------------------
1049 //----------------------------------------------------------------------
1051 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
1052 : wxPanel( frame
, wxID_ANY
, wxPoint(x
, y
), wxSize(w
, h
) )
1055 m_log
= new wxTextCtrl( this, wxID_ANY
, wxT("This is the log window.\n"),
1056 wxPoint(5,260), wxSize(630,100),
1057 wxTE_MULTILINE
| wxTE_READONLY
);
1059 m_logOld
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
1062 // single line text controls
1064 m_text
= new MyTextCtrl( this, wxID_ANY
, wxT("Single line."),
1065 wxDefaultPosition
, wxDefaultSize
,
1066 wxTE_PROCESS_ENTER
);
1067 m_text
->SetForegroundColour(*wxBLUE
);
1068 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
1069 (*m_text
) << wxT(" Appended.");
1070 m_text
->SetInsertionPoint(0);
1071 m_text
->WriteText( wxT("Prepended. ") );
1073 m_password
= new MyTextCtrl( this, wxID_ANY
, wxT(""),
1074 wxPoint(10,50), wxSize(140,wxDefaultCoord
), wxTE_PASSWORD
);
1076 m_limited
= new MyTextCtrl(this, wxID_ANY
, "",
1077 wxPoint(10, 90), wxDefaultSize
);
1078 m_limited
->SetHint("Max 8 ch");
1079 m_limited
->SetMaxLength(8);
1080 wxSize size2
= m_limited
->GetSizeFromTextSize(m_limited
->GetTextExtent("WWWWWWWW"));
1081 m_limited
->SetSizeHints(size2
, size2
);
1083 // multi line text controls
1085 wxString
string3L("Read only\nMultiline\nFitted size");
1086 m_readonly
= new MyTextCtrl( this, wxID_ANY
, string3L
,
1087 wxPoint(10, 120), wxDefaultSize
, wxTE_MULTILINE
| wxTE_READONLY
);
1088 wxWindowDC
dc(m_readonly
);
1089 size2
= m_readonly
->GetSizeFromTextSize(dc
.GetMultiLineTextExtent(string3L
));
1090 m_readonly
->SetMinSize(size2
);
1092 m_horizontal
= new MyTextCtrl( this, wxID_ANY
, wxT("Multiline text control with a horizontal scrollbar.\n"),
1093 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
1095 // a little hack to use the command line argument for encoding testing
1096 if ( wxTheApp
->argc
== 2 )
1098 switch ( (wxChar
)wxTheApp
->argv
[1][0] )
1101 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
1103 wxFONTENCODING_ISO8859_2
));
1104 m_horizontal
->AppendText(wxT("\256lu\273ou\350k\375 k\371\362 zb\354sile \350e\271tina \253\273"));
1108 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
1110 wxFONTENCODING_CP1251
));
1111 m_horizontal
->AppendText(wxT("\317\360\350\342\345\362!"));
1115 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
1117 wxFONTENCODING_CP1251
));
1119 m_horizontal
->AppendText(L
"\x0412\x0430\x0434\x0438\x043c \x0426");
1121 m_horizontal
->AppendText("\313\301\326\305\324\323\321 \325\304\301\336\316\331\315");
1127 m_horizontal
->AppendText(wxT("Text in default encoding"));
1130 m_multitext
= new MyTextCtrl( this, wxID_ANY
,
1131 wxT("Multi line without vertical scrollbar."),
1132 wxPoint(180,10), wxSize(200,70), wxTE_MULTILINE
| wxTE_NO_VSCROLL
);
1133 m_multitext
->SetFont(*wxITALIC_FONT
);
1134 (*m_multitext
) << wxT(" Appended.");
1135 m_multitext
->SetInsertionPoint(0);
1136 m_multitext
->WriteText( wxT("Prepended. ") );
1137 m_multitext
->SetForegroundColour(*wxYELLOW
);
1138 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
1141 m_multitext
->SetToolTip(wxT("Press Fn function keys here"));
1144 m_tab
= new MyTextCtrl( this, 100, wxT("Multiline, allow <TAB> processing."),
1145 wxPoint(180,90), wxDefaultSize
, wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
1146 m_tab
->SetClientData((void *)wxT("tab"));
1148 m_enter
= new MyTextCtrl( this, 100, wxT("Multiline, allow <ENTER> processing."),
1149 wxPoint(180,170), wxSize(200,70), wxTE_MULTILINE
);
1150 m_enter
->SetClientData((void *)wxT("enter"));
1152 m_textrich
= new MyTextCtrl(this, wxID_ANY
, wxT("Allows more than 30Kb of text\n")
1153 wxT("(even under broken Win9x)\n")
1154 wxT("and a very very very very very ")
1155 wxT("very very very long line to test ")
1156 wxT("wxHSCROLL style\n")
1157 wxT("\nAnd here is a link in quotation marks to ")
1158 wxT("test wxTE_AUTO_URL: \"http://www.wxwidgets.org\""),
1159 wxPoint(450, 10), wxSize(200, 230),
1160 wxTE_RICH
| wxTE_MULTILINE
| wxTE_AUTO_URL
);
1161 m_textrich
->SetStyle(0, 10, *wxRED
);
1162 m_textrich
->SetStyle(10, 20, *wxBLUE
);
1163 m_textrich
->SetStyle(30, 40,
1164 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
1165 m_textrich
->SetDefaultStyle(wxTextAttr());
1166 m_textrich
->AppendText(wxT("\n\nFirst 10 characters should be in red\n"));
1167 m_textrich
->AppendText(wxT("Next 10 characters should be in blue\n"));
1168 m_textrich
->AppendText(wxT("Next 10 characters should be normal\n"));
1169 m_textrich
->AppendText(wxT("And the next 10 characters should be green and italic\n"));
1170 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
1171 m_textrich
->AppendText(wxT("This text should be cyan on blue\n"));
1172 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
, *wxWHITE
));
1173 m_textrich
->AppendText(wxT("And this should be in blue and the text you ")
1174 wxT("type should be in blue as well"));
1177 // lay out the controls
1178 wxBoxSizer
*column1
= new wxBoxSizer(wxVERTICAL
);
1179 column1
->Add( m_text
, 0, wxALL
| wxEXPAND
, 10 );
1180 column1
->Add( m_password
, 0, wxALL
| wxEXPAND
, 10 );
1181 column1
->Add( m_readonly
, 0, wxALL
, 10 );
1182 column1
->Add( m_limited
, 0, wxALL
, 10 );
1183 column1
->Add( m_horizontal
, 1, wxALL
| wxEXPAND
, 10 );
1185 wxBoxSizer
*column2
= new wxBoxSizer(wxVERTICAL
);
1186 column2
->Add( m_multitext
, 1, wxALL
| wxEXPAND
, 10 );
1187 column2
->Add( m_tab
, 0, wxALL
| wxEXPAND
, 10 );
1188 column2
->Add( m_enter
, 1, wxALL
| wxEXPAND
, 10 );
1190 wxBoxSizer
*row1
= new wxBoxSizer(wxHORIZONTAL
);
1191 row1
->Add( column1
, 0, wxALL
| wxEXPAND
, 10 );
1192 row1
->Add( column2
, 1, wxALL
| wxEXPAND
, 10 );
1193 row1
->Add( m_textrich
, 1, wxALL
| wxEXPAND
, 10 );
1195 wxBoxSizer
*topSizer
= new wxBoxSizer(wxVERTICAL
);
1196 topSizer
->Add( row1
, 2, wxALL
| wxEXPAND
, 10 );
1199 topSizer
->Add( m_log
, 1, wxALL
| wxEXPAND
, 10 );
1205 wxTextCtrl
*MyPanel::GetFocusedText() const
1207 wxWindow
*win
= FindFocus();
1209 wxTextCtrl
*text
= win
? wxDynamicCast(win
, wxTextCtrl
) : NULL
;
1210 return text
? text
: m_multitext
;
1214 void MyPanel::DoPasteFromClipboard()
1216 // On X11, we want to get the data from the primary selection instead
1217 // of the normal clipboard (which isn't normal under X11 at all). This
1218 // call has no effect under MSW.
1219 wxTheClipboard
->UsePrimarySelection();
1221 if (!wxTheClipboard
->Open())
1224 *m_log
<< wxT("Error opening the clipboard.\n");
1231 *m_log
<< wxT("Successfully opened the clipboard.\n");
1235 wxTextDataObject data
;
1237 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
1240 *m_log
<< wxT("Clipboard supports requested format.\n");
1243 if (wxTheClipboard
->GetData( data
))
1246 *m_log
<< wxT("Successfully retrieved data from the clipboard.\n");
1248 GetFocusedText()->AppendText(data
.GetText());
1253 *m_log
<< wxT("Error getting data from the clipboard.\n");
1260 *m_log
<< wxT("Clipboard doesn't support requested format.\n");
1264 wxTheClipboard
->Close();
1267 *m_log
<< wxT("Closed the clipboard.\n");
1271 void MyPanel::DoCopyToClipboard()
1273 // On X11, we want to get the data from the primary selection instead
1274 // of the normal clipboard (which isn't normal under X11 at all). This
1275 // call has no effect under MSW.
1276 wxTheClipboard
->UsePrimarySelection();
1278 wxString
text( GetFocusedText()->GetStringSelection() );
1283 *m_log
<< wxT("No text to copy.\n");
1289 if (!wxTheClipboard
->Open())
1292 *m_log
<< wxT("Error opening the clipboard.\n");
1300 *m_log
<< wxT("Successfully opened the clipboard.\n");
1304 wxTextDataObject
*data
= new wxTextDataObject( text
);
1306 if (!wxTheClipboard
->SetData( data
))
1309 *m_log
<< wxT("Error while copying to the clipboard.\n");
1315 *m_log
<< wxT("Successfully copied data to the clipboard.\n");
1319 wxTheClipboard
->Close();
1322 *m_log
<< wxT("Closed the clipboard.\n");
1326 #endif // wxUSE_CLIPBOARD
1328 void MyPanel::DoMoveToEndOfText()
1330 m_multitext
->SetInsertionPointEnd();
1331 m_multitext
->SetFocus();
1334 void MyPanel::DoGetWindowCoordinates()
1336 wxTextCtrl
* const text
= GetFocusedText();
1338 const wxPoint pt0
= text
->PositionToCoords(0);
1339 const wxPoint ptCur
= text
->PositionToCoords(text
->GetInsertionPoint());
1340 *m_log
<< "Current position coordinates: "
1341 "(" << ptCur
.x
<< ", " << ptCur
.y
<< "), "
1342 "first position coordinates: "
1343 "(" << pt0
.x
<< ", " << pt0
.y
<< ")\n";
1346 void MyPanel::DoMoveToEndOfEntry()
1348 m_text
->SetInsertionPointEnd();
1352 void MyPanel::DoRemoveText()
1354 GetFocusedText()->Remove(0, 10);
1357 void MyPanel::DoReplaceText()
1359 GetFocusedText()->Replace(3, 8, wxT("ABC"));
1362 void MyPanel::DoSelectText()
1364 GetFocusedText()->SetSelection(3, 8);
1367 //----------------------------------------------------------------------
1369 //----------------------------------------------------------------------
1371 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1372 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
1373 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
1374 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
1375 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
1376 EVT_MENU(TEXT_RICH_TEXT_TEST
, MyFrame::OnRichTextTest
)
1378 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
1379 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
1380 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
1381 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
1382 EVT_MENU(TEXT_LOG_FOCUS
,MyFrame::OnLogFocus
)
1383 EVT_MENU(TEXT_LOG_CLIP
, MyFrame::OnLogClip
)
1385 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
1389 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
1390 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
1391 #endif // wxUSE_TOOLTIPS
1394 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
1395 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
1397 EVT_UPDATE_UI(TEXT_CLIPBOARD_PASTE
, MyFrame::OnUpdatePasteFromClipboard
)
1398 EVT_UPDATE_UI(TEXT_CLIPBOARD_COPY
, MyFrame::OnUpdateCopyToClipboard
)
1399 #endif // wxUSE_CLIPBOARD
1401 EVT_MENU(TEXT_REMOVE
, MyFrame::OnRemoveText
)
1402 EVT_MENU(TEXT_REPLACE
, MyFrame::OnReplaceText
)
1403 EVT_MENU(TEXT_SELECT
, MyFrame::OnSelectText
)
1404 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
1405 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
1406 EVT_MENU(TEXT_ADD_LINE
, MyFrame::OnAddTextLine
)
1407 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
1408 EVT_MENU(TEXT_GET_WINDOW_COORD
, MyFrame::OnGetWindowCoordinates
)
1409 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
1411 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
1412 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
1414 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
1415 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
1416 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
1417 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
1419 EVT_MENU(TEXT_GET_LINE
, MyFrame::OnGetLine
)
1420 EVT_MENU(TEXT_GET_LINELENGTH
, MyFrame::OnGetLineLength
)
1422 EVT_MENU(TEXT_SET
, MyFrame::OnSetText
)
1423 EVT_MENU(TEXT_CHANGE
, MyFrame::OnChangeText
)
1425 EVT_IDLE(MyFrame::OnIdle
)
1428 MyFrame::MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
)
1429 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
) )
1431 SetIcon(wxICON(sample
));
1435 #endif // wxUSE_STATUSBAR
1437 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
1440 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
1445 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1447 wxBeginBusyCursor();
1449 wxMessageDialog
dialog(this,
1450 wxT("This is a text control sample. It demonstrates the many different\n")
1451 wxT("text control styles, the use of the clipboard, setting and handling\n")
1452 wxT("tooltips and intercepting key and char events.\n")
1454 wxT("Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin"),
1455 wxT("About wxTextCtrl Sample"),
1456 wxOK
| wxICON_INFORMATION
);
1465 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& WXUNUSED(event
))
1467 static long s_delay
= 5000;
1470 delay
.Printf( wxT("%ld"), s_delay
);
1472 delay
= wxGetTextFromUser(wxT("Enter delay (in milliseconds)"),
1473 wxT("Set tooltip delay"),
1477 return; // cancelled
1479 wxSscanf(delay
, wxT("%ld"), &s_delay
);
1481 wxToolTip::SetDelay(s_delay
);
1483 wxLogStatus(this, wxT("Tooltip delay set to %ld milliseconds"), s_delay
);
1486 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
1488 static bool s_enabled
= true;
1490 s_enabled
= !s_enabled
;
1492 wxToolTip::Enable(s_enabled
);
1494 wxLogStatus(this, wxT("Tooltips %sabled"), s_enabled
? wxT("en") : wxT("dis") );
1499 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1501 m_panel
->m_log
->Clear();
1505 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1507 static bool s_editable
= true;
1509 s_editable
= !s_editable
;
1510 m_panel
->m_text
->SetEditable(s_editable
);
1511 m_panel
->m_password
->SetEditable(s_editable
);
1512 m_panel
->m_multitext
->SetEditable(s_editable
);
1513 m_panel
->m_textrich
->SetEditable(s_editable
);
1516 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1518 bool enabled
= m_panel
->m_text
->IsEnabled();
1521 m_panel
->m_text
->Enable(enabled
);
1522 m_panel
->m_password
->Enable(enabled
);
1523 m_panel
->m_multitext
->Enable(enabled
);
1524 m_panel
->m_readonly
->Enable(enabled
);
1525 m_panel
->m_limited
->Enable(enabled
);
1526 m_panel
->m_textrich
->Enable(enabled
);
1529 void MyFrame::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1531 if ( m_panel
->m_textrich
->SaveFile(wxT("dummy.txt")) )
1534 // verify that the fil length is correct (it wasn't under Win95)
1535 wxFile
file(wxT("dummy.txt"));
1537 wxT("Successfully saved file (text len = %lu, file size = %ld)"),
1538 (unsigned long)m_panel
->m_textrich
->GetValue().length(),
1539 (long) file
.Length());
1543 wxLogStatus(this, wxT("Couldn't save the file"));
1546 void MyFrame::OnFileLoad(wxCommandEvent
& WXUNUSED(event
))
1548 if ( m_panel
->m_textrich
->LoadFile(wxT("dummy.txt")) )
1550 wxLogStatus(this, wxT("Successfully loaded file"));
1554 wxLogStatus(this, wxT("Couldn't load the file"));
1558 void MyFrame::OnRichTextTest(wxCommandEvent
& WXUNUSED(event
))
1560 RichTextFrame
* frame
= new RichTextFrame(this, wxT("Rich Text Editor"));
1564 void MyFrame::OnIdle( wxIdleEvent
& event
)
1566 // track the window which has the focus in the status bar
1567 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1568 wxWindow
*focus
= wxWindow::FindFocus();
1569 if ( focus
&& (focus
!= s_windowFocus
) )
1571 s_windowFocus
= focus
;
1576 wxT("Focus: wxWindow = %p, HWND = %p"),
1578 wxT("Focus: wxWindow = %p"),
1582 , s_windowFocus
->GetHWND()
1588 #endif // wxUSE_STATUSBAR
1594 * RichTextFrame is used to demonstrate rich text behaviour
1599 RICHTEXT_CLOSE
= 1000,
1600 RICHTEXT_LEFT_ALIGN
,
1601 RICHTEXT_RIGHT_ALIGN
,
1604 RICHTEXT_CHANGE_FONT
,
1605 RICHTEXT_CHANGE_TEXT_COLOUR
,
1606 RICHTEXT_CHANGE_BACKGROUND_COLOUR
,
1607 RICHTEXT_LEFT_INDENT
,
1608 RICHTEXT_RIGHT_INDENT
,
1612 BEGIN_EVENT_TABLE(RichTextFrame
, wxFrame
)
1613 EVT_IDLE(RichTextFrame::OnIdle
)
1614 EVT_MENU(RICHTEXT_CLOSE
, RichTextFrame::OnClose
)
1615 EVT_MENU(RICHTEXT_LEFT_ALIGN
, RichTextFrame::OnLeftAlign
)
1616 EVT_MENU(RICHTEXT_RIGHT_ALIGN
, RichTextFrame::OnRightAlign
)
1617 EVT_MENU(RICHTEXT_CENTRE
, RichTextFrame::OnCentre
)
1618 EVT_MENU(RICHTEXT_JUSTIFY
, RichTextFrame::OnJustify
)
1619 EVT_MENU(RICHTEXT_CHANGE_FONT
, RichTextFrame::OnChangeFont
)
1620 EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR
, RichTextFrame::OnChangeTextColour
)
1621 EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, RichTextFrame::OnChangeBackgroundColour
)
1622 EVT_MENU(RICHTEXT_LEFT_INDENT
, RichTextFrame::OnLeftIndent
)
1623 EVT_MENU(RICHTEXT_RIGHT_INDENT
, RichTextFrame::OnRightIndent
)
1624 EVT_MENU(RICHTEXT_TAB_STOPS
, RichTextFrame::OnTabStops
)
1627 RichTextFrame::RichTextFrame(wxWindow
* parent
, const wxString
& title
):
1628 wxFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(300, 400))
1630 m_currentPosition
= -1;
1631 m_textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1632 wxDefaultSize
, wxTE_MULTILINE
|wxTE_RICH2
);
1636 for (i
= 0; i
< 10; i
++)
1639 for (j
= 0; j
< 10; j
++)
1641 value
<< wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
1643 value
<< wxT("\n\n");
1645 m_textCtrl
->SetValue(value
);
1647 wxMenuBar
* menuBar
= new wxMenuBar
;
1648 wxMenu
* fileMenu
= new wxMenu
;
1649 fileMenu
->Append(RICHTEXT_CLOSE
, _("Close\tCtrl+W"));
1650 menuBar
->Append(fileMenu
, _("File"));
1652 wxMenu
* editMenu
= new wxMenu
;
1653 editMenu
->Append(RICHTEXT_LEFT_ALIGN
, _("Left Align"));
1654 editMenu
->Append(RICHTEXT_RIGHT_ALIGN
, _("Right Align"));
1655 editMenu
->Append(RICHTEXT_CENTRE
, _("Centre"));
1656 editMenu
->Append(RICHTEXT_JUSTIFY
, _("Justify"));
1657 editMenu
->AppendSeparator();
1658 editMenu
->Append(RICHTEXT_CHANGE_FONT
, _("Change Font"));
1659 editMenu
->Append(RICHTEXT_CHANGE_TEXT_COLOUR
, _("Change Text Colour"));
1660 editMenu
->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, _("Change Background Colour"));
1661 editMenu
->AppendSeparator();
1662 editMenu
->Append(RICHTEXT_LEFT_INDENT
, _("Left Indent"));
1663 editMenu
->Append(RICHTEXT_RIGHT_INDENT
, _("Right Indent"));
1664 editMenu
->Append(RICHTEXT_TAB_STOPS
, _("Tab Stops"));
1665 menuBar
->Append(editMenu
, _("Edit"));
1667 SetMenuBar(menuBar
);
1670 #endif // wxUSE_STATUSBAR
1675 void RichTextFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
1680 void RichTextFrame::OnLeftAlign(wxCommandEvent
& WXUNUSED(event
))
1683 attr
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
1686 m_textCtrl
->GetSelection(& start
, & end
);
1687 m_textCtrl
->SetStyle(start
, end
, attr
);
1689 m_currentPosition
= -1;
1692 void RichTextFrame::OnRightAlign(wxCommandEvent
& WXUNUSED(event
))
1695 attr
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1698 m_textCtrl
->GetSelection(& start
, & end
);
1699 m_textCtrl
->SetStyle(start
, end
, attr
);
1701 m_currentPosition
= -1;
1704 void RichTextFrame::OnJustify(wxCommandEvent
& WXUNUSED(event
))
1707 attr
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
1710 m_textCtrl
->GetSelection(& start
, & end
);
1711 m_textCtrl
->SetStyle(start
, end
, attr
);
1713 m_currentPosition
= -1;
1716 void RichTextFrame::OnCentre(wxCommandEvent
& WXUNUSED(event
))
1719 attr
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1722 m_textCtrl
->GetSelection(& start
, & end
);
1723 m_textCtrl
->SetStyle(start
, end
, attr
);
1725 m_currentPosition
= -1;
1728 void RichTextFrame::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
1732 wxFontDialog
dialog(this, data
);
1734 if (dialog
.ShowModal() == wxID_OK
)
1736 wxFontData retData
= dialog
.GetFontData();
1737 wxFont font
= retData
.GetChosenFont();
1743 m_textCtrl
->GetSelection(& start
, & end
);
1744 m_textCtrl
->SetStyle(start
, end
, attr
);
1746 m_currentPosition
= -1;
1750 void RichTextFrame::OnChangeTextColour(wxCommandEvent
& WXUNUSED(event
))
1753 data
.SetColour(* wxBLACK
);
1754 data
.SetChooseFull(true);
1755 for (int i
= 0; i
< 16; i
++)
1757 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1758 data
.SetCustomColour(i
, colour
);
1761 wxColourDialog
dialog(this, &data
);
1762 dialog
.SetTitle(wxT("Choose the text colour"));
1763 if (dialog
.ShowModal() == wxID_OK
)
1765 wxColourData retData
= dialog
.GetColourData();
1766 wxColour col
= retData
.GetColour();
1769 attr
.SetTextColour(col
);
1772 m_textCtrl
->GetSelection(& start
, & end
);
1773 m_textCtrl
->SetStyle(start
, end
, attr
);
1775 m_currentPosition
= -1;
1779 void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
1782 data
.SetColour(* wxWHITE
);
1783 data
.SetChooseFull(true);
1784 for (int i
= 0; i
< 16; i
++)
1786 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1787 data
.SetCustomColour(i
, colour
);
1790 wxColourDialog
dialog(this, &data
);
1791 dialog
.SetTitle(wxT("Choose the text background colour"));
1792 if (dialog
.ShowModal() == wxID_OK
)
1794 wxColourData retData
= dialog
.GetColourData();
1795 wxColour col
= retData
.GetColour();
1798 attr
.SetBackgroundColour(col
);
1801 m_textCtrl
->GetSelection(& start
, & end
);
1802 m_textCtrl
->SetStyle(start
, end
, attr
);
1804 m_currentPosition
= -1;
1808 void RichTextFrame::OnLeftIndent(wxCommandEvent
& WXUNUSED(event
))
1810 wxString indentStr
= wxGetTextFromUser
1812 _("Please enter the left indent in tenths of a millimetre."),
1817 if (!indentStr
.IsEmpty())
1819 int indent
= wxAtoi(indentStr
);
1822 attr
.SetLeftIndent(indent
);
1825 m_textCtrl
->GetSelection(& start
, & end
);
1826 m_textCtrl
->SetStyle(start
, end
, attr
);
1828 m_currentPosition
= -1;
1832 void RichTextFrame::OnRightIndent(wxCommandEvent
& WXUNUSED(event
))
1834 wxString indentStr
= wxGetTextFromUser
1836 _("Please enter the right indent in tenths of a millimetre."),
1841 if (!indentStr
.IsEmpty())
1843 int indent
= wxAtoi(indentStr
);
1846 attr
.SetRightIndent(indent
);
1849 m_textCtrl
->GetSelection(& start
, & end
);
1850 m_textCtrl
->SetStyle(start
, end
, attr
);
1852 m_currentPosition
= -1;
1856 void RichTextFrame::OnTabStops(wxCommandEvent
& WXUNUSED(event
))
1858 wxString tabsStr
= wxGetTextFromUser
1860 _("Please enter the tab stop positions in tenths of a millimetre, separated by spaces.\nLeave empty to reset tab stops."),
1868 wxStringTokenizer
tokens(tabsStr
, wxT(" "));
1869 while (tokens
.HasMoreTokens())
1871 wxString token
= tokens
.GetNextToken();
1872 tabs
.Add(wxAtoi(token
));
1879 m_textCtrl
->GetSelection(& start
, & end
);
1880 m_textCtrl
->SetStyle(start
, end
, attr
);
1882 m_currentPosition
= -1;
1885 void RichTextFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1887 long insertionPoint
= m_textCtrl
->GetInsertionPoint();
1888 if (insertionPoint
!= m_currentPosition
)
1892 if (m_textCtrl
->GetStyle(insertionPoint
, attr
))
1895 wxString
facename(wxT("unknown"));
1896 if (attr
.GetFont().IsOk())
1898 facename
= attr
.GetFont().GetFaceName();
1900 wxString
alignment(wxT("unknown alignment"));
1901 if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1902 alignment
= wxT("centred");
1903 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
1904 alignment
= wxT("right-aligned");
1905 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1906 alignment
= wxT("left-aligned");
1907 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
1908 alignment
= wxT("justified");
1910 msg
.Printf("Facename: %s", facename
);
1912 if (attr
.HasTextColour())
1914 msg
+= wxString::Format(", colour: %s",
1915 attr
.GetTextColour().GetAsString());
1919 msg
+= ", no colour";
1922 msg
<< ", " << alignment
;
1926 if (attr
.GetFont().GetWeight() == wxBOLD
)
1927 msg
+= wxT(" BOLD");
1928 else if (attr
.GetFont().GetWeight() == wxNORMAL
)
1929 msg
+= wxT(" NORMAL");
1931 if (attr
.GetFont().GetStyle() == wxITALIC
)
1932 msg
+= wxT(" ITALIC");
1934 if (attr
.GetFont().GetUnderlined())
1935 msg
+= wxT(" UNDERLINED");
1940 #endif // wxUSE_STATUSBAR
1941 m_currentPosition
= insertionPoint
;