1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: TextCtrl wxWidgets sample
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
23 #include "wx/dataobj.h"
24 #include "wx/clipbrd.h"
32 #include "wx/tooltip.h"
35 // We test for wxUSE_DRAG_AND_DROP also, because data objects may not be
36 // implemented for compilers that can't cope with the OLE parts in
37 // wxUSE_DRAG_AND_DROP.
38 #if !wxUSE_DRAG_AND_DROP
39 #undef wxUSE_CLIPBOARD
40 #define wxUSE_CLIPBOARD 0
43 #include "wx/colordlg.h"
44 #include "wx/fontdlg.h"
45 #include "wx/numdlg.h"
46 #include "wx/tokenzr.h"
48 #ifndef wxHAS_IMAGES_IN_RESOURCES
49 #include "../sample.xpm"
52 //----------------------------------------------------------------------
54 //----------------------------------------------------------------------
56 class MyApp
: public wxApp
62 // a text ctrl which allows to call different wxTextCtrl functions
63 // interactively by pressing function keys in it
64 class MyTextCtrl
: public wxTextCtrl
67 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
68 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
69 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
)
74 void OnKeyDown(wxKeyEvent
& event
);
75 void OnKeyUp(wxKeyEvent
& event
);
76 void OnChar(wxKeyEvent
& event
);
78 void OnText(wxCommandEvent
& event
);
79 void OnTextEnter(wxCommandEvent
& event
);
80 void OnTextURL(wxTextUrlEvent
& event
);
81 void OnTextMaxLen(wxCommandEvent
& event
);
83 void OnTextCut(wxClipboardTextEvent
& event
);
84 void OnTextCopy(wxClipboardTextEvent
& event
);
85 void OnTextPaste(wxClipboardTextEvent
& event
);
87 void OnMouseEvent(wxMouseEvent
& event
);
89 void OnSetFocus(wxFocusEvent
& event
);
90 void OnKillFocus(wxFocusEvent
& event
);
92 static bool ms_logKey
;
93 static bool ms_logChar
;
94 static bool ms_logMouse
;
95 static bool ms_logText
;
96 static bool ms_logFocus
;
97 static bool ms_logClip
;
100 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: wxT('-'); }
102 void LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
103 void LogClipEvent(const wxChar
*what
, wxClipboardTextEvent
& event
);
107 DECLARE_EVENT_TABLE()
110 class MyPanel
: public wxPanel
113 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
117 delete wxLog::SetActiveTarget(m_logOld
);
122 void DoPasteFromClipboard();
123 void DoCopyToClipboard();
124 #endif // wxUSE_CLIPBOARD
127 void DoReplaceText();
129 void DoMoveToEndOfText();
130 void DoMoveToEndOfEntry();
131 void DoGetWindowCoordinates();
133 // return true if currently text control has any selection
134 bool HasSelection() const
137 GetFocusedText()->GetSelection(&from
, &to
);
142 MyTextCtrl
*m_password
;
145 MyTextCtrl
*m_readonly
;
146 MyTextCtrl
*m_limited
;
148 MyTextCtrl
*m_multitext
;
149 MyTextCtrl
*m_horizontal
;
151 MyTextCtrl
*m_textrich
;
159 // get the currently focused text control or return the default one
160 // (m_multitext) is no text ctrl has focus -- in any case, returns
161 // something non NULL
162 wxTextCtrl
*GetFocusedText() const;
165 class MyFrame
: public wxFrame
168 MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
170 void OnQuit(wxCommandEvent
& event
);
171 void OnAbout(wxCommandEvent
& event
);
173 void OnSetTooltipDelay(wxCommandEvent
& event
);
174 void OnToggleTooltips(wxCommandEvent
& event
);
175 #endif // wxUSE_TOOLTIPS
178 void OnPasteFromClipboard( wxCommandEvent
& WXUNUSED(event
) )
180 wxLogMessage(wxT("Pasting text from clipboard."));
181 m_panel
->DoPasteFromClipboard();
183 void OnCopyToClipboard( wxCommandEvent
& WXUNUSED(event
) )
185 wxLogMessage(wxT("Copying text to clipboard."));
186 m_panel
->DoCopyToClipboard();
189 void OnUpdatePasteFromClipboard(wxUpdateUIEvent
& event
)
191 wxClipboardLocker lockClip
;
193 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
196 void OnUpdateCopyToClipboard(wxUpdateUIEvent
& event
)
198 event
.Enable( m_panel
->HasSelection() );
200 #endif // wxUSE_CLIPBOARD
202 void OnAddTextLine(wxCommandEvent
& WXUNUSED(event
))
205 m_panel
->m_textrich
->AppendText(wxString::Format("Line %d\n", ++s_n
));
208 void OnAddTextFreeze( wxCommandEvent
& WXUNUSED(event
) )
210 void OnAddText( wxCommandEvent
& WXUNUSED(event
) )
211 { DoAddText(false); }
212 void OnRemoveText( wxCommandEvent
& WXUNUSED(event
) )
213 { m_panel
->DoRemoveText(); }
214 void OnReplaceText( wxCommandEvent
& WXUNUSED(event
) )
215 { m_panel
->DoReplaceText(); }
216 void OnSelectText( wxCommandEvent
& WXUNUSED(event
) )
217 { m_panel
->DoSelectText(); }
219 void OnMoveToEndOfText( wxCommandEvent
& WXUNUSED(event
) )
220 { m_panel
->DoMoveToEndOfText(); }
222 void OnGetWindowCoordinates( wxCommandEvent
& WXUNUSED(event
) )
223 { m_panel
->DoGetWindowCoordinates(); }
225 void OnMoveToEndOfEntry( wxCommandEvent
& WXUNUSED(event
) )
226 { m_panel
->DoMoveToEndOfEntry(); }
228 void OnScrollLineDown(wxCommandEvent
& WXUNUSED(event
))
230 if ( !m_panel
->m_textrich
->LineDown() )
232 wxLogMessage(wxT("Already at the bottom"));
236 void OnScrollLineUp(wxCommandEvent
& WXUNUSED(event
))
238 if ( !m_panel
->m_textrich
->LineUp() )
240 wxLogMessage(wxT("Already at the top"));
244 void OnScrollPageDown(wxCommandEvent
& WXUNUSED(event
))
246 if ( !m_panel
->m_textrich
->PageDown() )
248 wxLogMessage(wxT("Already at the bottom"));
252 void OnScrollPageUp(wxCommandEvent
& WXUNUSED(event
))
254 if ( !m_panel
->m_textrich
->PageUp() )
256 wxLogMessage(wxT("Already at the top"));
260 void OnGetLine(wxCommandEvent
& WXUNUSED(event
))
262 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
263 wxT("Enter which line you would like to get"),
264 wxT("Get a line from the tabbed multiline text control") );
266 wxMessageBox(m_panel
->m_tab
->GetLineText(nLine
));
269 void OnGetLineLength(wxCommandEvent
& WXUNUSED(event
))
271 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
272 wxT("Enter which line you would like to get"),
273 wxT("Get length of a line from the tabbed multiline text control") );
275 wxMessageBox(wxString::Format(wxT("Length of line %i is:%i"),
277 m_panel
->m_tab
->GetLineLength(nLine
))
282 void OnLogClear(wxCommandEvent
& event
);
284 void OnFileSave(wxCommandEvent
& event
);
285 void OnFileLoad(wxCommandEvent
& event
);
286 void OnRichTextTest(wxCommandEvent
& event
);
288 void OnSetEditable(wxCommandEvent
& event
);
289 void OnSetEnabled(wxCommandEvent
& event
);
291 void OnLogKey(wxCommandEvent
& event
)
293 MyTextCtrl::ms_logKey
= event
.IsChecked();
296 void OnLogChar(wxCommandEvent
& event
)
298 MyTextCtrl::ms_logChar
= event
.IsChecked();
301 void OnLogMouse(wxCommandEvent
& event
)
303 MyTextCtrl::ms_logMouse
= event
.IsChecked();
306 void OnLogText(wxCommandEvent
& event
)
308 MyTextCtrl::ms_logText
= event
.IsChecked();
311 void OnLogFocus(wxCommandEvent
& event
)
313 MyTextCtrl::ms_logFocus
= event
.IsChecked();
316 void OnLogClip(wxCommandEvent
& event
)
318 MyTextCtrl::ms_logClip
= event
.IsChecked();
321 void OnSetText(wxCommandEvent
& WXUNUSED(event
))
323 m_panel
->m_text
->SetValue(wxT("Hello, world! (what else did you expect?)"));
326 void OnChangeText(wxCommandEvent
& WXUNUSED(event
))
328 m_panel
->m_text
->ChangeValue(wxT("Changed, not set: no event"));
331 void OnIdle( wxIdleEvent
& event
);
334 void DoAddText(bool freeze
)
336 wxTextCtrl
* const text
= m_panel
->m_textrich
;
343 for ( int i
= 0; i
< 100; i
++ )
345 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
348 text
->SetInsertionPoint(0);
356 DECLARE_EVENT_TABLE()
360 * RichTextFrame is used to demonstrate rich text behaviour
363 class RichTextFrame
: public wxFrame
366 RichTextFrame(wxWindow
* parent
, const wxString
& title
);
370 void OnClose(wxCommandEvent
& event
);
371 void OnIdle(wxIdleEvent
& event
);
372 void OnLeftAlign(wxCommandEvent
& event
);
373 void OnRightAlign(wxCommandEvent
& event
);
374 void OnJustify(wxCommandEvent
& event
);
375 void OnCentre(wxCommandEvent
& event
);
376 void OnChangeFont(wxCommandEvent
& event
);
377 void OnChangeTextColour(wxCommandEvent
& event
);
378 void OnChangeBackgroundColour(wxCommandEvent
& event
);
379 void OnLeftIndent(wxCommandEvent
& event
);
380 void OnRightIndent(wxCommandEvent
& event
);
381 void OnTabStops(wxCommandEvent
& event
);
384 wxTextCtrl
*m_textCtrl
;
385 long m_currentPosition
;
387 DECLARE_EVENT_TABLE()
390 //----------------------------------------------------------------------
392 //----------------------------------------------------------------------
396 //----------------------------------------------------------------------
398 //----------------------------------------------------------------------
402 TEXT_QUIT
= wxID_EXIT
,
403 TEXT_ABOUT
= wxID_ABOUT
,
410 TEXT_CLIPBOARD_COPY
= 200,
411 TEXT_CLIPBOARD_PASTE
,
415 TEXT_TOOLTIPS_SETDELAY
= 300,
416 TEXT_TOOLTIPS_ENABLE
,
423 TEXT_GET_WINDOW_COORD
,
454 if ( !wxApp::OnInit() )
457 // Create the main frame window
458 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
459 wxT("Text wxWidgets sample"), 50, 50, 700, 550);
460 frame
->SetSizeHints( 500, 400 );
462 wxMenu
*file_menu
= new wxMenu
;
463 file_menu
->Append(TEXT_SAVE
, wxT("&Save file\tCtrl-S"),
464 wxT("Save the text control contents to file"));
465 file_menu
->Append(TEXT_LOAD
, wxT("&Load file\tCtrl-O"),
466 wxT("Load the sample file into text control"));
467 file_menu
->AppendSeparator();
468 file_menu
->Append(TEXT_RICH_TEXT_TEST
, wxT("Show Rich Text Editor"));
469 file_menu
->AppendSeparator();
470 file_menu
->Append(TEXT_ABOUT
, wxT("&About\tAlt-A"));
471 file_menu
->AppendSeparator();
472 file_menu
->Append(TEXT_QUIT
, wxT("E&xit\tAlt-X"), wxT("Quit this sample"));
474 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
475 menu_bar
->Append(file_menu
, wxT("&File"));
478 wxMenu
*tooltip_menu
= new wxMenu
;
479 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, wxT("Set &delay\tCtrl-D"));
480 tooltip_menu
->AppendSeparator();
481 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, wxT("&Toggle tooltips\tCtrl-T"),
482 wxT("enable/disable tooltips"), true);
483 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, true);
484 menu_bar
->Append(tooltip_menu
, wxT("&Tooltips"));
485 #endif // wxUSE_TOOLTIPS
488 // notice that we use non default accelerators on purpose here to compare
489 // their behaviour with the built in handling of standard Ctrl/Cmd-C/V
490 wxMenu
*menuClipboard
= new wxMenu
;
491 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, wxT("&Copy\tCtrl-Shift-C"),
492 wxT("Copy the selection to the clipboard"));
493 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, wxT("&Paste\tCtrl-Shift-V"),
494 wxT("Paste from clipboard to the text control"));
495 menuClipboard
->AppendSeparator();
496 menuClipboard
->AppendCheckItem(TEXT_CLIPBOARD_VETO
, wxT("Vet&o\tCtrl-Shift-O"),
497 wxT("Veto all clipboard operations"));
498 menu_bar
->Append(menuClipboard
, wxT("&Clipboard"));
499 #endif // wxUSE_CLIPBOARD
501 wxMenu
*menuText
= new wxMenu
;
502 menuText
->Append(TEXT_ADD_SOME
, wxT("&Append some text\tCtrl-A"));
503 menuText
->Append(TEXT_ADD_FREEZE
, wxT("&Append text with freeze/thaw\tShift-Ctrl-A"));
504 menuText
->Append(TEXT_ADD_LINE
, wxT("Append a new &line\tAlt-Shift-A"));
505 menuText
->Append(TEXT_REMOVE
, wxT("&Remove first 10 characters\tCtrl-Y"));
506 menuText
->Append(TEXT_REPLACE
, wxT("&Replace characters 4 to 8 with ABC\tCtrl-R"));
507 menuText
->Append(TEXT_SELECT
, wxT("&Select characters 4 to 8\tCtrl-I"));
508 menuText
->Append(TEXT_SET
, wxT("&Set the first text zone value\tCtrl-E"));
509 menuText
->Append(TEXT_CHANGE
, wxT("&Change the first text zone value\tShift-Ctrl-E"));
510 menuText
->AppendSeparator();
511 menuText
->Append(TEXT_MOVE_ENDTEXT
, wxT("Move cursor to the end of &text"));
512 menuText
->Append(TEXT_MOVE_ENDENTRY
, wxT("Move cursor to the end of &entry"));
513 menuText
->AppendCheckItem(TEXT_SET_EDITABLE
, wxT("Toggle &editable state"));
514 menuText
->AppendCheckItem(TEXT_SET_ENABLED
, wxT("Toggle e&nabled state"));
515 menuText
->Check(TEXT_SET_EDITABLE
, true);
516 menuText
->Check(TEXT_SET_ENABLED
, true);
517 menuText
->AppendSeparator();
518 menuText
->Append(TEXT_LINE_DOWN
, wxT("Scroll text one line down"));
519 menuText
->Append(TEXT_LINE_UP
, wxT("Scroll text one line up"));
520 menuText
->Append(TEXT_PAGE_DOWN
, wxT("Scroll text one page down"));
521 menuText
->Append(TEXT_PAGE_UP
, wxT("Scroll text one page up"));
522 menuText
->Append(TEXT_GET_WINDOW_COORD
, wxT("Get window coordinates"));
523 menuText
->AppendSeparator();
524 menuText
->Append(TEXT_GET_LINE
, wxT("Get the text of a line of the tabbed multiline"));
525 menuText
->Append(TEXT_GET_LINELENGTH
, wxT("Get the length of a line of the tabbed multiline"));
526 menu_bar
->Append(menuText
, wxT("Te&xt"));
529 wxMenu
*menuLog
= new wxMenu
;
530 menuLog
->AppendCheckItem(TEXT_LOG_KEY
, wxT("Log &key events"));
531 menuLog
->AppendCheckItem(TEXT_LOG_CHAR
, wxT("Log &char events"));
532 menuLog
->AppendCheckItem(TEXT_LOG_MOUSE
, wxT("Log &mouse events"));
533 menuLog
->AppendCheckItem(TEXT_LOG_TEXT
, wxT("Log &text events"));
534 menuLog
->AppendCheckItem(TEXT_LOG_FOCUS
, wxT("Log &focus events"));
535 menuLog
->AppendCheckItem(TEXT_LOG_CLIP
, wxT("Log clip&board events"));
536 menuLog
->AppendSeparator();
537 menuLog
->Append(TEXT_CLEAR
, wxT("&Clear the log\tCtrl-L"),
538 wxT("Clear the log window contents"));
540 // select only the interesting events by default
541 MyTextCtrl::ms_logClip
=
542 MyTextCtrl::ms_logText
= true;
544 menuLog
->Check(TEXT_LOG_KEY
, MyTextCtrl::ms_logKey
);
545 menuLog
->Check(TEXT_LOG_CHAR
, MyTextCtrl::ms_logChar
);
546 menuLog
->Check(TEXT_LOG_TEXT
, MyTextCtrl::ms_logText
);
548 menu_bar
->Append(menuLog
, wxT("&Log"));
551 frame
->SetMenuBar(menu_bar
);
559 //----------------------------------------------------------------------
561 //----------------------------------------------------------------------
563 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
564 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
565 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
566 EVT_CHAR(MyTextCtrl::OnChar
)
568 EVT_TEXT(wxID_ANY
, MyTextCtrl::OnText
)
569 EVT_TEXT_ENTER(wxID_ANY
, MyTextCtrl::OnTextEnter
)
570 EVT_TEXT_URL(wxID_ANY
, MyTextCtrl::OnTextURL
)
571 EVT_TEXT_MAXLEN(wxID_ANY
, MyTextCtrl::OnTextMaxLen
)
572 EVT_TEXT_CUT(wxID_ANY
, MyTextCtrl::OnTextCut
)
573 EVT_TEXT_COPY(wxID_ANY
, MyTextCtrl::OnTextCopy
)
574 EVT_TEXT_PASTE(wxID_ANY
, MyTextCtrl::OnTextPaste
)
576 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
578 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus
)
579 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus
)
582 bool MyTextCtrl::ms_logKey
= false;
583 bool MyTextCtrl::ms_logChar
= false;
584 bool MyTextCtrl::ms_logMouse
= false;
585 bool MyTextCtrl::ms_logText
= false;
586 bool MyTextCtrl::ms_logFocus
= false;
587 bool MyTextCtrl::ms_logClip
= false;
589 void MyTextCtrl::LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const
592 long keycode
= event
.GetKeyCode();
596 case WXK_BACK
: key
= wxT("BACK"); break;
597 case WXK_TAB
: key
= wxT("TAB"); break;
598 case WXK_RETURN
: key
= wxT("RETURN"); break;
599 case WXK_ESCAPE
: key
= wxT("ESCAPE"); break;
600 case WXK_SPACE
: key
= wxT("SPACE"); break;
601 case WXK_DELETE
: key
= wxT("DELETE"); break;
602 case WXK_START
: key
= wxT("START"); break;
603 case WXK_LBUTTON
: key
= wxT("LBUTTON"); break;
604 case WXK_RBUTTON
: key
= wxT("RBUTTON"); break;
605 case WXK_CANCEL
: key
= wxT("CANCEL"); break;
606 case WXK_MBUTTON
: key
= wxT("MBUTTON"); break;
607 case WXK_CLEAR
: key
= wxT("CLEAR"); break;
608 case WXK_SHIFT
: key
= wxT("SHIFT"); break;
609 case WXK_ALT
: key
= wxT("ALT"); break;
610 case WXK_CONTROL
: key
= wxT("CONTROL"); break;
611 case WXK_MENU
: key
= wxT("MENU"); break;
612 case WXK_PAUSE
: key
= wxT("PAUSE"); break;
613 case WXK_CAPITAL
: key
= wxT("CAPITAL"); break;
614 case WXK_END
: key
= wxT("END"); break;
615 case WXK_HOME
: key
= wxT("HOME"); break;
616 case WXK_LEFT
: key
= wxT("LEFT"); break;
617 case WXK_UP
: key
= wxT("UP"); break;
618 case WXK_RIGHT
: key
= wxT("RIGHT"); break;
619 case WXK_DOWN
: key
= wxT("DOWN"); break;
620 case WXK_SELECT
: key
= wxT("SELECT"); break;
621 case WXK_PRINT
: key
= wxT("PRINT"); break;
622 case WXK_EXECUTE
: key
= wxT("EXECUTE"); break;
623 case WXK_SNAPSHOT
: key
= wxT("SNAPSHOT"); break;
624 case WXK_INSERT
: key
= wxT("INSERT"); break;
625 case WXK_HELP
: key
= wxT("HELP"); break;
626 case WXK_NUMPAD0
: key
= wxT("NUMPAD0"); break;
627 case WXK_NUMPAD1
: key
= wxT("NUMPAD1"); break;
628 case WXK_NUMPAD2
: key
= wxT("NUMPAD2"); break;
629 case WXK_NUMPAD3
: key
= wxT("NUMPAD3"); break;
630 case WXK_NUMPAD4
: key
= wxT("NUMPAD4"); break;
631 case WXK_NUMPAD5
: key
= wxT("NUMPAD5"); break;
632 case WXK_NUMPAD6
: key
= wxT("NUMPAD6"); break;
633 case WXK_NUMPAD7
: key
= wxT("NUMPAD7"); break;
634 case WXK_NUMPAD8
: key
= wxT("NUMPAD8"); break;
635 case WXK_NUMPAD9
: key
= wxT("NUMPAD9"); break;
636 case WXK_MULTIPLY
: key
= wxT("MULTIPLY"); break;
637 case WXK_ADD
: key
= wxT("ADD"); break;
638 case WXK_SEPARATOR
: key
= wxT("SEPARATOR"); break;
639 case WXK_SUBTRACT
: key
= wxT("SUBTRACT"); break;
640 case WXK_DECIMAL
: key
= wxT("DECIMAL"); break;
641 case WXK_DIVIDE
: key
= wxT("DIVIDE"); break;
642 case WXK_F1
: key
= wxT("F1"); break;
643 case WXK_F2
: key
= wxT("F2"); break;
644 case WXK_F3
: key
= wxT("F3"); break;
645 case WXK_F4
: key
= wxT("F4"); break;
646 case WXK_F5
: key
= wxT("F5"); break;
647 case WXK_F6
: key
= wxT("F6"); break;
648 case WXK_F7
: key
= wxT("F7"); break;
649 case WXK_F8
: key
= wxT("F8"); break;
650 case WXK_F9
: key
= wxT("F9"); break;
651 case WXK_F10
: key
= wxT("F10"); break;
652 case WXK_F11
: key
= wxT("F11"); break;
653 case WXK_F12
: key
= wxT("F12"); break;
654 case WXK_F13
: key
= wxT("F13"); break;
655 case WXK_F14
: key
= wxT("F14"); break;
656 case WXK_F15
: key
= wxT("F15"); break;
657 case WXK_F16
: key
= wxT("F16"); break;
658 case WXK_F17
: key
= wxT("F17"); break;
659 case WXK_F18
: key
= wxT("F18"); break;
660 case WXK_F19
: key
= wxT("F19"); break;
661 case WXK_F20
: key
= wxT("F20"); break;
662 case WXK_F21
: key
= wxT("F21"); break;
663 case WXK_F22
: key
= wxT("F22"); break;
664 case WXK_F23
: key
= wxT("F23"); break;
665 case WXK_F24
: key
= wxT("F24"); break;
666 case WXK_NUMLOCK
: key
= wxT("NUMLOCK"); break;
667 case WXK_SCROLL
: key
= wxT("SCROLL"); break;
668 case WXK_PAGEUP
: key
= wxT("PAGEUP"); break;
669 case WXK_PAGEDOWN
: key
= wxT("PAGEDOWN"); break;
670 case WXK_NUMPAD_SPACE
: key
= wxT("NUMPAD_SPACE"); break;
671 case WXK_NUMPAD_TAB
: key
= wxT("NUMPAD_TAB"); break;
672 case WXK_NUMPAD_ENTER
: key
= wxT("NUMPAD_ENTER"); break;
673 case WXK_NUMPAD_F1
: key
= wxT("NUMPAD_F1"); break;
674 case WXK_NUMPAD_F2
: key
= wxT("NUMPAD_F2"); break;
675 case WXK_NUMPAD_F3
: key
= wxT("NUMPAD_F3"); break;
676 case WXK_NUMPAD_F4
: key
= wxT("NUMPAD_F4"); break;
677 case WXK_NUMPAD_HOME
: key
= wxT("NUMPAD_HOME"); break;
678 case WXK_NUMPAD_LEFT
: key
= wxT("NUMPAD_LEFT"); break;
679 case WXK_NUMPAD_UP
: key
= wxT("NUMPAD_UP"); break;
680 case WXK_NUMPAD_RIGHT
: key
= wxT("NUMPAD_RIGHT"); break;
681 case WXK_NUMPAD_DOWN
: key
= wxT("NUMPAD_DOWN"); break;
682 case WXK_NUMPAD_PAGEUP
: key
= wxT("NUMPAD_PAGEUP"); break;
683 case WXK_NUMPAD_PAGEDOWN
: key
= wxT("NUMPAD_PAGEDOWN"); break;
684 case WXK_NUMPAD_END
: key
= wxT("NUMPAD_END"); break;
685 case WXK_NUMPAD_BEGIN
: key
= wxT("NUMPAD_BEGIN"); break;
686 case WXK_NUMPAD_INSERT
: key
= wxT("NUMPAD_INSERT"); break;
687 case WXK_NUMPAD_DELETE
: key
= wxT("NUMPAD_DELETE"); break;
688 case WXK_NUMPAD_EQUAL
: key
= wxT("NUMPAD_EQUAL"); break;
689 case WXK_NUMPAD_MULTIPLY
: key
= wxT("NUMPAD_MULTIPLY"); break;
690 case WXK_NUMPAD_ADD
: key
= wxT("NUMPAD_ADD"); break;
691 case WXK_NUMPAD_SEPARATOR
: key
= wxT("NUMPAD_SEPARATOR"); break;
692 case WXK_NUMPAD_SUBTRACT
: key
= wxT("NUMPAD_SUBTRACT"); break;
693 case WXK_NUMPAD_DECIMAL
: key
= wxT("NUMPAD_DECIMAL"); break;
697 if ( wxIsprint((int)keycode
) )
698 key
.Printf(wxT("'%c'"), (char)keycode
);
699 else if ( keycode
> 0 && keycode
< 27 )
700 key
.Printf(_("Ctrl-%c"), wxT('A') + keycode
- 1);
702 key
.Printf(wxT("unknown (%ld)"), keycode
);
708 key
+= wxString::Format(wxT(" (Unicode: %#04x)"), event
.GetUnicodeKey());
709 #endif // wxUSE_UNICODE
711 wxLogMessage( wxT("%s event: %s (flags = %c%c%c%c)"),
714 GetChar( event
.ControlDown(), wxT('C') ),
715 GetChar( event
.AltDown(), wxT('A') ),
716 GetChar( event
.ShiftDown(), wxT('S') ),
717 GetChar( event
.MetaDown(), wxT('M') ) );
720 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
725 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
727 button
= wxT("Left");
728 dbl
= ev
.LeftDClick();
731 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
733 button
= wxT("Middle");
734 dbl
= ev
.MiddleDClick();
737 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
739 button
= wxT("Right");
740 dbl
= ev
.RightDClick();
743 else if ( ev
.Aux1Down() || ev
.Aux1Up() || ev
.Aux1DClick() )
745 button
= wxT("Aux1");
746 dbl
= ev
.Aux1DClick();
749 else if ( ev
.Aux2Down() || ev
.Aux2Up() || ev
.Aux2DClick() )
751 button
= wxT("Aux2");
752 dbl
= ev
.Aux2DClick();
755 else if ( ev
.GetWheelRotation() )
757 return wxString::Format("Wheel rotation %+d", ev
.GetWheelRotation());
761 return wxT("Unknown mouse event");
763 wxASSERT(!(dbl
&& up
));
765 return wxString::Format(wxT("%s mouse button %s"),
767 dbl
? wxT("double clicked")
768 : up
? wxT("released") : wxT("clicked"));
771 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
783 msg
= wxT("Mouse entered the window");
785 else if ( ev
.Leaving() )
787 msg
= wxT("Mouse left the window");
791 msg
= GetMouseEventDesc(ev
);
794 msg
<< wxT(" at (") << ev
.GetX() << wxT(", ") << ev
.GetY() << wxT(") ");
797 wxTextCtrlHitTestResult rc
= HitTest(ev
.GetPosition(), &pos
);
798 if ( rc
!= wxTE_HT_UNKNOWN
)
800 msg
<< wxT("at position ") << pos
<< wxT(' ');
803 msg
<< wxT("[Flags: ")
804 << GetChar( ev
.LeftIsDown(), wxT('1') )
805 << GetChar( ev
.MiddleIsDown(), wxT('2') )
806 << GetChar( ev
.RightIsDown(), wxT('3') )
807 << GetChar( ev
.Aux1IsDown(), wxT('x') )
808 << GetChar( ev
.Aux2IsDown(), wxT('X') )
809 << GetChar( ev
.ControlDown(), wxT('C') )
810 << GetChar( ev
.AltDown(), wxT('A') )
811 << GetChar( ev
.ShiftDown(), wxT('S') )
812 << GetChar( ev
.MetaDown(), wxT('M') )
817 //else: we're not interested in mouse move events
820 void MyTextCtrl::OnSetFocus(wxFocusEvent
& event
)
824 wxLogMessage( wxT("%p got focus."), this);
830 void MyTextCtrl::OnKillFocus(wxFocusEvent
& event
)
834 wxLogMessage( wxT("%p lost focus"), this);
840 void MyTextCtrl::OnText(wxCommandEvent
& event
)
845 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
846 const wxChar
*changeVerb
= win
->IsModified() ? wxT("changed")
847 : wxT("set by program");
848 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
851 wxLogMessage(wxT("Text %s in control \"%s\""), changeVerb
, data
);
855 wxLogMessage(wxT("Text %s in some control"), changeVerb
);
859 void MyTextCtrl::OnTextEnter(wxCommandEvent
& event
)
864 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
865 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
868 wxLogMessage(wxT("Enter pressed in control '%s'"), data
);
872 wxLogMessage(wxT("Enter pressed in some control"));
876 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& WXUNUSED(event
))
878 wxLogMessage(wxT("You can't enter more characters into this control."));
882 void MyTextCtrl::OnTextCut(wxClipboardTextEvent
& event
)
884 LogClipEvent(wxT("cut to"), event
);
887 void MyTextCtrl::OnTextCopy(wxClipboardTextEvent
& event
)
889 LogClipEvent(wxT("copied to"), event
);
892 void MyTextCtrl::OnTextPaste(wxClipboardTextEvent
& event
)
894 LogClipEvent(wxT("pasted from"), event
);
897 void MyTextCtrl::LogClipEvent(const wxChar
*what
, wxClipboardTextEvent
& event
)
899 wxFrame
*frame
= wxDynamicCast(wxGetTopLevelParent(this), wxFrame
);
900 wxCHECK_RET( frame
, wxT("no parent frame?") );
902 const bool veto
= frame
->GetMenuBar()->IsChecked(TEXT_CLIPBOARD_VETO
);
908 wxLogMessage(wxT("Text %s%s the clipboard."),
909 veto
? wxT("not ") : wxT(""), what
);
914 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
916 const wxMouseEvent
& ev
= event
.GetMouseEvent();
918 // filter out mouse moves, too many of them
922 long start
= event
.GetURLStart(),
923 end
= event
.GetURLEnd();
925 wxLogMessage(wxT("Mouse event over URL '%s': %s"),
926 GetValue().Mid(start
, end
- start
).c_str(),
927 GetMouseEventDesc(ev
).c_str());
930 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
933 LogKeyEvent( wxT("Char"), event
);
938 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
941 LogKeyEvent( wxT("Key up"), event
);
946 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
948 switch ( event
.GetKeyCode() )
951 // show current position and text length
953 long line
, column
, pos
= GetInsertionPoint();
954 PositionToXY(pos
, &column
, &line
);
956 wxLogMessage(wxT("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
959 (long) GetNumberOfLines(),
960 (long) GetLineLength(line
),
961 (unsigned int) GetValue().length(),
965 GetSelection(&from
, &to
);
967 wxString sel
= GetStringSelection();
969 wxLogMessage(wxT("Selection: from %ld to %ld."), from
, to
);
970 wxLogMessage(wxT("Selection = '%s' (len = %u)"),
972 (unsigned int) sel
.length());
974 const wxString text
= GetLineText(line
);
975 wxLogMessage(wxT("Current line: \"%s\"; length = %lu"),
976 text
.c_str(), text
.length());
982 SetInsertionPointEnd();
987 SetInsertionPoint(10);
993 wxLogDebug( wxT("Now capturing mouse and events.") );
999 wxLogDebug( wxT("Stopped capturing mouse and events.") );
1000 m_hasCapture
= false;
1006 // insert a blank line
1007 WriteText(wxT("\n"));
1011 wxLogMessage(wxT("IsModified() before SetValue(): %d"),
1013 ChangeValue(wxT("ChangeValue() has been called"));
1014 wxLogMessage(wxT("IsModified() after SetValue(): %d"),
1019 wxLogMessage(wxT("Position 10 should be now visible."));
1024 wxLogMessage(wxT("Control has been cleared"));
1029 WriteText(wxT("WriteText() has been called"));
1033 AppendText(wxT("AppendText() has been called"));
1038 wxLogMessage(wxT("Control marked as non modified"));
1043 LogKeyEvent( wxT("Key down"), event
);
1048 //----------------------------------------------------------------------
1050 //----------------------------------------------------------------------
1052 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
1053 : wxPanel( frame
, wxID_ANY
, wxPoint(x
, y
), wxSize(w
, h
) )
1056 m_log
= new wxTextCtrl( this, wxID_ANY
, wxT("This is the log window.\n"),
1057 wxPoint(5,260), wxSize(630,100),
1058 wxTE_MULTILINE
| wxTE_READONLY
);
1060 m_logOld
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
1063 // single line text controls
1065 m_text
= new MyTextCtrl( this, wxID_ANY
, wxT("Single line."),
1066 wxDefaultPosition
, wxDefaultSize
,
1067 wxTE_PROCESS_ENTER
);
1068 m_text
->SetForegroundColour(*wxBLUE
);
1069 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
1070 (*m_text
) << wxT(" Appended.");
1071 m_text
->SetInsertionPoint(0);
1072 m_text
->WriteText( wxT("Prepended. ") );
1074 m_password
= new MyTextCtrl( this, wxID_ANY
, wxT(""),
1075 wxPoint(10,50), wxSize(140,wxDefaultCoord
), wxTE_PASSWORD
);
1077 m_readonly
= new MyTextCtrl( this, wxID_ANY
, wxT("Read only"),
1078 wxPoint(10,90), wxSize(140,wxDefaultCoord
), wxTE_READONLY
);
1080 m_limited
= new MyTextCtrl(this, wxID_ANY
, "",
1081 wxPoint(10, 130), wxSize(140, wxDefaultCoord
));
1082 m_limited
->SetHint("Max 8 ch");
1083 m_limited
->SetMaxLength(8);
1085 // multi line text controls
1087 m_horizontal
= new MyTextCtrl( this, wxID_ANY
, wxT("Multiline text control with a horizontal scrollbar.\n"),
1088 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
1090 // a little hack to use the command line argument for encoding testing
1091 if ( wxTheApp
->argc
== 2 )
1093 switch ( (wxChar
)wxTheApp
->argv
[1][0] )
1096 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
1098 wxFONTENCODING_ISO8859_2
));
1099 m_horizontal
->AppendText(wxT("\256lu\273ou\350k\375 k\371\362 zb\354sile \350e\271tina \253\273"));
1103 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
1105 wxFONTENCODING_CP1251
));
1106 m_horizontal
->AppendText(wxT("\317\360\350\342\345\362!"));
1110 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
1112 wxFONTENCODING_CP1251
));
1114 m_horizontal
->AppendText(L
"\x0412\x0430\x0434\x0438\x043c \x0426");
1116 m_horizontal
->AppendText("\313\301\326\305\324\323\321 \325\304\301\336\316\331\315");
1122 m_horizontal
->AppendText(wxT("Text in default encoding"));
1125 m_multitext
= new MyTextCtrl( this, wxID_ANY
,
1126 wxT("Multi line without vertical scrollbar."),
1127 wxPoint(180,10), wxSize(200,70), wxTE_MULTILINE
| wxTE_NO_VSCROLL
);
1128 m_multitext
->SetFont(*wxITALIC_FONT
);
1129 (*m_multitext
) << wxT(" Appended.");
1130 m_multitext
->SetInsertionPoint(0);
1131 m_multitext
->WriteText( wxT("Prepended. ") );
1132 m_multitext
->SetForegroundColour(*wxYELLOW
);
1133 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
1136 m_multitext
->SetToolTip(wxT("Press Fn function keys here"));
1139 m_tab
= new MyTextCtrl( this, 100, wxT("Multiline, allow <TAB> processing."),
1140 wxPoint(180,90), wxSize(200,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
1141 m_tab
->SetClientData((void *)wxT("tab"));
1143 m_enter
= new MyTextCtrl( this, 100, wxT("Multiline, allow <ENTER> processing."),
1144 wxPoint(180,170), wxSize(200,70), wxTE_MULTILINE
);
1145 m_enter
->SetClientData((void *)wxT("enter"));
1147 m_textrich
= new MyTextCtrl(this, wxID_ANY
, wxT("Allows more than 30Kb of text\n")
1148 wxT("(even under broken Win9x)\n")
1149 wxT("and a very very very very very ")
1150 wxT("very very very long line to test ")
1151 wxT("wxHSCROLL style\n")
1152 wxT("\nAnd here is a link in quotation marks to ")
1153 wxT("test wxTE_AUTO_URL: \"http://www.wxwidgets.org\""),
1154 wxPoint(450, 10), wxSize(200, 230),
1155 wxTE_RICH
| wxTE_MULTILINE
| wxTE_AUTO_URL
);
1156 m_textrich
->SetStyle(0, 10, *wxRED
);
1157 m_textrich
->SetStyle(10, 20, *wxBLUE
);
1158 m_textrich
->SetStyle(30, 40,
1159 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
1160 m_textrich
->SetDefaultStyle(wxTextAttr());
1161 m_textrich
->AppendText(wxT("\n\nFirst 10 characters should be in red\n"));
1162 m_textrich
->AppendText(wxT("Next 10 characters should be in blue\n"));
1163 m_textrich
->AppendText(wxT("Next 10 characters should be normal\n"));
1164 m_textrich
->AppendText(wxT("And the next 10 characters should be green and italic\n"));
1165 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
1166 m_textrich
->AppendText(wxT("This text should be cyan on blue\n"));
1167 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
, *wxWHITE
));
1168 m_textrich
->AppendText(wxT("And this should be in blue and the text you ")
1169 wxT("type should be in blue as well"));
1172 // lay out the controls
1173 wxBoxSizer
*column1
= new wxBoxSizer(wxVERTICAL
);
1174 column1
->Add( m_text
, 0, wxALL
| wxEXPAND
, 10 );
1175 column1
->Add( m_password
, 0, wxALL
| wxEXPAND
, 10 );
1176 column1
->Add( m_readonly
, 0, wxALL
| wxEXPAND
, 10 );
1177 column1
->Add( m_limited
, 0, wxALL
| wxEXPAND
, 10 );
1178 column1
->Add( m_horizontal
, 1, wxALL
| wxEXPAND
, 10 );
1180 wxBoxSizer
*column2
= new wxBoxSizer(wxVERTICAL
);
1181 column2
->Add( m_multitext
, 1, wxALL
| wxEXPAND
, 10 );
1182 column2
->Add( m_tab
, 1, wxALL
| wxEXPAND
, 10 );
1183 column2
->Add( m_enter
, 1, wxALL
| wxEXPAND
, 10 );
1185 wxBoxSizer
*row1
= new wxBoxSizer(wxHORIZONTAL
);
1186 row1
->Add( column1
, 0, wxALL
| wxEXPAND
, 10 );
1187 row1
->Add( column2
, 1, wxALL
| wxEXPAND
, 10 );
1188 row1
->Add( m_textrich
, 1, wxALL
| wxEXPAND
, 10 );
1190 wxBoxSizer
*topSizer
= new wxBoxSizer(wxVERTICAL
);
1191 topSizer
->Add( row1
, 2, wxALL
| wxEXPAND
, 10 );
1194 topSizer
->Add( m_log
, 1, wxALL
| wxEXPAND
, 10 );
1200 wxTextCtrl
*MyPanel::GetFocusedText() const
1202 wxWindow
*win
= FindFocus();
1204 wxTextCtrl
*text
= win
? wxDynamicCast(win
, wxTextCtrl
) : NULL
;
1205 return text
? text
: m_multitext
;
1209 void MyPanel::DoPasteFromClipboard()
1211 // On X11, we want to get the data from the primary selection instead
1212 // of the normal clipboard (which isn't normal under X11 at all). This
1213 // call has no effect under MSW.
1214 wxTheClipboard
->UsePrimarySelection();
1216 if (!wxTheClipboard
->Open())
1219 *m_log
<< wxT("Error opening the clipboard.\n");
1226 *m_log
<< wxT("Successfully opened the clipboard.\n");
1230 wxTextDataObject data
;
1232 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
1235 *m_log
<< wxT("Clipboard supports requested format.\n");
1238 if (wxTheClipboard
->GetData( data
))
1241 *m_log
<< wxT("Successfully retrieved data from the clipboard.\n");
1243 GetFocusedText()->AppendText(data
.GetText());
1248 *m_log
<< wxT("Error getting data from the clipboard.\n");
1255 *m_log
<< wxT("Clipboard doesn't support requested format.\n");
1259 wxTheClipboard
->Close();
1262 *m_log
<< wxT("Closed the clipboard.\n");
1266 void MyPanel::DoCopyToClipboard()
1268 // On X11, we want to get the data from the primary selection instead
1269 // of the normal clipboard (which isn't normal under X11 at all). This
1270 // call has no effect under MSW.
1271 wxTheClipboard
->UsePrimarySelection();
1273 wxString
text( GetFocusedText()->GetStringSelection() );
1278 *m_log
<< wxT("No text to copy.\n");
1284 if (!wxTheClipboard
->Open())
1287 *m_log
<< wxT("Error opening the clipboard.\n");
1295 *m_log
<< wxT("Successfully opened the clipboard.\n");
1299 wxTextDataObject
*data
= new wxTextDataObject( text
);
1301 if (!wxTheClipboard
->SetData( data
))
1304 *m_log
<< wxT("Error while copying to the clipboard.\n");
1310 *m_log
<< wxT("Successfully copied data to the clipboard.\n");
1314 wxTheClipboard
->Close();
1317 *m_log
<< wxT("Closed the clipboard.\n");
1321 #endif // wxUSE_CLIPBOARD
1323 void MyPanel::DoMoveToEndOfText()
1325 m_multitext
->SetInsertionPointEnd();
1326 m_multitext
->SetFocus();
1329 void MyPanel::DoGetWindowCoordinates()
1331 wxTextCtrl
* const text
= GetFocusedText();
1333 const wxPoint pt0
= text
->PositionToCoords(0);
1334 const wxPoint ptCur
= text
->PositionToCoords(text
->GetInsertionPoint());
1335 *m_log
<< "Current position coordinates: "
1336 "(" << ptCur
.x
<< ", " << ptCur
.y
<< "), "
1337 "first position coordinates: "
1338 "(" << pt0
.x
<< ", " << pt0
.y
<< ")\n";
1341 void MyPanel::DoMoveToEndOfEntry()
1343 m_text
->SetInsertionPointEnd();
1347 void MyPanel::DoRemoveText()
1349 GetFocusedText()->Remove(0, 10);
1352 void MyPanel::DoReplaceText()
1354 GetFocusedText()->Replace(3, 8, wxT("ABC"));
1357 void MyPanel::DoSelectText()
1359 GetFocusedText()->SetSelection(3, 8);
1362 //----------------------------------------------------------------------
1364 //----------------------------------------------------------------------
1366 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1367 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
1368 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
1369 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
1370 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
1371 EVT_MENU(TEXT_RICH_TEXT_TEST
, MyFrame::OnRichTextTest
)
1373 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
1374 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
1375 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
1376 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
1377 EVT_MENU(TEXT_LOG_FOCUS
,MyFrame::OnLogFocus
)
1378 EVT_MENU(TEXT_LOG_CLIP
, MyFrame::OnLogClip
)
1380 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
1384 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
1385 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
1386 #endif // wxUSE_TOOLTIPS
1389 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
1390 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
1392 EVT_UPDATE_UI(TEXT_CLIPBOARD_PASTE
, MyFrame::OnUpdatePasteFromClipboard
)
1393 EVT_UPDATE_UI(TEXT_CLIPBOARD_COPY
, MyFrame::OnUpdateCopyToClipboard
)
1394 #endif // wxUSE_CLIPBOARD
1396 EVT_MENU(TEXT_REMOVE
, MyFrame::OnRemoveText
)
1397 EVT_MENU(TEXT_REPLACE
, MyFrame::OnReplaceText
)
1398 EVT_MENU(TEXT_SELECT
, MyFrame::OnSelectText
)
1399 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
1400 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
1401 EVT_MENU(TEXT_ADD_LINE
, MyFrame::OnAddTextLine
)
1402 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
1403 EVT_MENU(TEXT_GET_WINDOW_COORD
, MyFrame::OnGetWindowCoordinates
)
1404 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
1406 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
1407 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
1409 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
1410 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
1411 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
1412 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
1414 EVT_MENU(TEXT_GET_LINE
, MyFrame::OnGetLine
)
1415 EVT_MENU(TEXT_GET_LINELENGTH
, MyFrame::OnGetLineLength
)
1417 EVT_MENU(TEXT_SET
, MyFrame::OnSetText
)
1418 EVT_MENU(TEXT_CHANGE
, MyFrame::OnChangeText
)
1420 EVT_IDLE(MyFrame::OnIdle
)
1423 MyFrame::MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
)
1424 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
) )
1426 SetIcon(wxICON(sample
));
1430 #endif // wxUSE_STATUSBAR
1432 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
1435 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
1440 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1442 wxBeginBusyCursor();
1444 wxMessageDialog
dialog(this,
1445 wxT("This is a text control sample. It demonstrates the many different\n")
1446 wxT("text control styles, the use of the clipboard, setting and handling\n")
1447 wxT("tooltips and intercepting key and char events.\n")
1449 wxT("Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin"),
1450 wxT("About wxTextCtrl Sample"),
1451 wxOK
| wxICON_INFORMATION
);
1460 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& WXUNUSED(event
))
1462 static long s_delay
= 5000;
1465 delay
.Printf( wxT("%ld"), s_delay
);
1467 delay
= wxGetTextFromUser(wxT("Enter delay (in milliseconds)"),
1468 wxT("Set tooltip delay"),
1472 return; // cancelled
1474 wxSscanf(delay
, wxT("%ld"), &s_delay
);
1476 wxToolTip::SetDelay(s_delay
);
1478 wxLogStatus(this, wxT("Tooltip delay set to %ld milliseconds"), s_delay
);
1481 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
1483 static bool s_enabled
= true;
1485 s_enabled
= !s_enabled
;
1487 wxToolTip::Enable(s_enabled
);
1489 wxLogStatus(this, wxT("Tooltips %sabled"), s_enabled
? wxT("en") : wxT("dis") );
1494 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1496 m_panel
->m_log
->Clear();
1500 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1502 static bool s_editable
= true;
1504 s_editable
= !s_editable
;
1505 m_panel
->m_text
->SetEditable(s_editable
);
1506 m_panel
->m_password
->SetEditable(s_editable
);
1507 m_panel
->m_multitext
->SetEditable(s_editable
);
1508 m_panel
->m_textrich
->SetEditable(s_editable
);
1511 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1513 bool enabled
= m_panel
->m_text
->IsEnabled();
1516 m_panel
->m_text
->Enable(enabled
);
1517 m_panel
->m_password
->Enable(enabled
);
1518 m_panel
->m_multitext
->Enable(enabled
);
1519 m_panel
->m_readonly
->Enable(enabled
);
1520 m_panel
->m_limited
->Enable(enabled
);
1521 m_panel
->m_textrich
->Enable(enabled
);
1524 void MyFrame::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1526 if ( m_panel
->m_textrich
->SaveFile(wxT("dummy.txt")) )
1529 // verify that the fil length is correct (it wasn't under Win95)
1530 wxFile
file(wxT("dummy.txt"));
1532 wxT("Successfully saved file (text len = %lu, file size = %ld)"),
1533 (unsigned long)m_panel
->m_textrich
->GetValue().length(),
1534 (long) file
.Length());
1538 wxLogStatus(this, wxT("Couldn't save the file"));
1541 void MyFrame::OnFileLoad(wxCommandEvent
& WXUNUSED(event
))
1543 if ( m_panel
->m_textrich
->LoadFile(wxT("dummy.txt")) )
1545 wxLogStatus(this, wxT("Successfully loaded file"));
1549 wxLogStatus(this, wxT("Couldn't load the file"));
1553 void MyFrame::OnRichTextTest(wxCommandEvent
& WXUNUSED(event
))
1555 RichTextFrame
* frame
= new RichTextFrame(this, wxT("Rich Text Editor"));
1559 void MyFrame::OnIdle( wxIdleEvent
& event
)
1561 // track the window which has the focus in the status bar
1562 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1563 wxWindow
*focus
= wxWindow::FindFocus();
1564 if ( focus
&& (focus
!= s_windowFocus
) )
1566 s_windowFocus
= focus
;
1571 wxT("Focus: wxWindow = %p, HWND = %p"),
1573 wxT("Focus: wxWindow = %p"),
1577 , s_windowFocus
->GetHWND()
1583 #endif // wxUSE_STATUSBAR
1589 * RichTextFrame is used to demonstrate rich text behaviour
1594 RICHTEXT_CLOSE
= 1000,
1595 RICHTEXT_LEFT_ALIGN
,
1596 RICHTEXT_RIGHT_ALIGN
,
1599 RICHTEXT_CHANGE_FONT
,
1600 RICHTEXT_CHANGE_TEXT_COLOUR
,
1601 RICHTEXT_CHANGE_BACKGROUND_COLOUR
,
1602 RICHTEXT_LEFT_INDENT
,
1603 RICHTEXT_RIGHT_INDENT
,
1607 BEGIN_EVENT_TABLE(RichTextFrame
, wxFrame
)
1608 EVT_IDLE(RichTextFrame::OnIdle
)
1609 EVT_MENU(RICHTEXT_CLOSE
, RichTextFrame::OnClose
)
1610 EVT_MENU(RICHTEXT_LEFT_ALIGN
, RichTextFrame::OnLeftAlign
)
1611 EVT_MENU(RICHTEXT_RIGHT_ALIGN
, RichTextFrame::OnRightAlign
)
1612 EVT_MENU(RICHTEXT_CENTRE
, RichTextFrame::OnCentre
)
1613 EVT_MENU(RICHTEXT_JUSTIFY
, RichTextFrame::OnJustify
)
1614 EVT_MENU(RICHTEXT_CHANGE_FONT
, RichTextFrame::OnChangeFont
)
1615 EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR
, RichTextFrame::OnChangeTextColour
)
1616 EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, RichTextFrame::OnChangeBackgroundColour
)
1617 EVT_MENU(RICHTEXT_LEFT_INDENT
, RichTextFrame::OnLeftIndent
)
1618 EVT_MENU(RICHTEXT_RIGHT_INDENT
, RichTextFrame::OnRightIndent
)
1619 EVT_MENU(RICHTEXT_TAB_STOPS
, RichTextFrame::OnTabStops
)
1622 RichTextFrame::RichTextFrame(wxWindow
* parent
, const wxString
& title
):
1623 wxFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(300, 400))
1625 m_currentPosition
= -1;
1626 m_textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1627 wxDefaultSize
, wxTE_MULTILINE
|wxTE_RICH2
);
1631 for (i
= 0; i
< 10; i
++)
1634 for (j
= 0; j
< 10; j
++)
1636 value
<< wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
1638 value
<< wxT("\n\n");
1640 m_textCtrl
->SetValue(value
);
1642 wxMenuBar
* menuBar
= new wxMenuBar
;
1643 wxMenu
* fileMenu
= new wxMenu
;
1644 fileMenu
->Append(RICHTEXT_CLOSE
, _("Close\tCtrl+W"));
1645 menuBar
->Append(fileMenu
, _("File"));
1647 wxMenu
* editMenu
= new wxMenu
;
1648 editMenu
->Append(RICHTEXT_LEFT_ALIGN
, _("Left Align"));
1649 editMenu
->Append(RICHTEXT_RIGHT_ALIGN
, _("Right Align"));
1650 editMenu
->Append(RICHTEXT_CENTRE
, _("Centre"));
1651 editMenu
->Append(RICHTEXT_JUSTIFY
, _("Justify"));
1652 editMenu
->AppendSeparator();
1653 editMenu
->Append(RICHTEXT_CHANGE_FONT
, _("Change Font"));
1654 editMenu
->Append(RICHTEXT_CHANGE_TEXT_COLOUR
, _("Change Text Colour"));
1655 editMenu
->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, _("Change Background Colour"));
1656 editMenu
->AppendSeparator();
1657 editMenu
->Append(RICHTEXT_LEFT_INDENT
, _("Left Indent"));
1658 editMenu
->Append(RICHTEXT_RIGHT_INDENT
, _("Right Indent"));
1659 editMenu
->Append(RICHTEXT_TAB_STOPS
, _("Tab Stops"));
1660 menuBar
->Append(editMenu
, _("Edit"));
1662 SetMenuBar(menuBar
);
1665 #endif // wxUSE_STATUSBAR
1670 void RichTextFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
1675 void RichTextFrame::OnLeftAlign(wxCommandEvent
& WXUNUSED(event
))
1678 attr
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
1681 m_textCtrl
->GetSelection(& start
, & end
);
1682 m_textCtrl
->SetStyle(start
, end
, attr
);
1684 m_currentPosition
= -1;
1687 void RichTextFrame::OnRightAlign(wxCommandEvent
& WXUNUSED(event
))
1690 attr
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1693 m_textCtrl
->GetSelection(& start
, & end
);
1694 m_textCtrl
->SetStyle(start
, end
, attr
);
1696 m_currentPosition
= -1;
1699 void RichTextFrame::OnJustify(wxCommandEvent
& WXUNUSED(event
))
1702 attr
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
1705 m_textCtrl
->GetSelection(& start
, & end
);
1706 m_textCtrl
->SetStyle(start
, end
, attr
);
1708 m_currentPosition
= -1;
1711 void RichTextFrame::OnCentre(wxCommandEvent
& WXUNUSED(event
))
1714 attr
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1717 m_textCtrl
->GetSelection(& start
, & end
);
1718 m_textCtrl
->SetStyle(start
, end
, attr
);
1720 m_currentPosition
= -1;
1723 void RichTextFrame::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
1727 wxFontDialog
dialog(this, data
);
1729 if (dialog
.ShowModal() == wxID_OK
)
1731 wxFontData retData
= dialog
.GetFontData();
1732 wxFont font
= retData
.GetChosenFont();
1738 m_textCtrl
->GetSelection(& start
, & end
);
1739 m_textCtrl
->SetStyle(start
, end
, attr
);
1741 m_currentPosition
= -1;
1745 void RichTextFrame::OnChangeTextColour(wxCommandEvent
& WXUNUSED(event
))
1748 data
.SetColour(* wxBLACK
);
1749 data
.SetChooseFull(true);
1750 for (int i
= 0; i
< 16; i
++)
1752 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1753 data
.SetCustomColour(i
, colour
);
1756 wxColourDialog
dialog(this, &data
);
1757 dialog
.SetTitle(wxT("Choose the text colour"));
1758 if (dialog
.ShowModal() == wxID_OK
)
1760 wxColourData retData
= dialog
.GetColourData();
1761 wxColour col
= retData
.GetColour();
1764 attr
.SetTextColour(col
);
1767 m_textCtrl
->GetSelection(& start
, & end
);
1768 m_textCtrl
->SetStyle(start
, end
, attr
);
1770 m_currentPosition
= -1;
1774 void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
1777 data
.SetColour(* wxWHITE
);
1778 data
.SetChooseFull(true);
1779 for (int i
= 0; i
< 16; i
++)
1781 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1782 data
.SetCustomColour(i
, colour
);
1785 wxColourDialog
dialog(this, &data
);
1786 dialog
.SetTitle(wxT("Choose the text background colour"));
1787 if (dialog
.ShowModal() == wxID_OK
)
1789 wxColourData retData
= dialog
.GetColourData();
1790 wxColour col
= retData
.GetColour();
1793 attr
.SetBackgroundColour(col
);
1796 m_textCtrl
->GetSelection(& start
, & end
);
1797 m_textCtrl
->SetStyle(start
, end
, attr
);
1799 m_currentPosition
= -1;
1803 void RichTextFrame::OnLeftIndent(wxCommandEvent
& WXUNUSED(event
))
1805 wxString indentStr
= wxGetTextFromUser
1807 _("Please enter the left indent in tenths of a millimetre."),
1812 if (!indentStr
.IsEmpty())
1814 int indent
= wxAtoi(indentStr
);
1817 attr
.SetLeftIndent(indent
);
1820 m_textCtrl
->GetSelection(& start
, & end
);
1821 m_textCtrl
->SetStyle(start
, end
, attr
);
1823 m_currentPosition
= -1;
1827 void RichTextFrame::OnRightIndent(wxCommandEvent
& WXUNUSED(event
))
1829 wxString indentStr
= wxGetTextFromUser
1831 _("Please enter the right indent in tenths of a millimetre."),
1836 if (!indentStr
.IsEmpty())
1838 int indent
= wxAtoi(indentStr
);
1841 attr
.SetRightIndent(indent
);
1844 m_textCtrl
->GetSelection(& start
, & end
);
1845 m_textCtrl
->SetStyle(start
, end
, attr
);
1847 m_currentPosition
= -1;
1851 void RichTextFrame::OnTabStops(wxCommandEvent
& WXUNUSED(event
))
1853 wxString tabsStr
= wxGetTextFromUser
1855 _("Please enter the tab stop positions in tenths of a millimetre, separated by spaces.\nLeave empty to reset tab stops."),
1863 wxStringTokenizer
tokens(tabsStr
, wxT(" "));
1864 while (tokens
.HasMoreTokens())
1866 wxString token
= tokens
.GetNextToken();
1867 tabs
.Add(wxAtoi(token
));
1874 m_textCtrl
->GetSelection(& start
, & end
);
1875 m_textCtrl
->SetStyle(start
, end
, attr
);
1877 m_currentPosition
= -1;
1880 void RichTextFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1882 long insertionPoint
= m_textCtrl
->GetInsertionPoint();
1883 if (insertionPoint
!= m_currentPosition
)
1887 if (m_textCtrl
->GetStyle(insertionPoint
, attr
))
1890 wxString
facename(wxT("unknown"));
1891 if (attr
.GetFont().IsOk())
1893 facename
= attr
.GetFont().GetFaceName();
1895 wxString
alignment(wxT("unknown alignment"));
1896 if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1897 alignment
= wxT("centred");
1898 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
1899 alignment
= wxT("right-aligned");
1900 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1901 alignment
= wxT("left-aligned");
1902 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
1903 alignment
= wxT("justified");
1905 msg
.Printf("Facename: %s", facename
);
1907 if (attr
.HasTextColour())
1909 msg
+= wxString::Format(", colour: %s",
1910 attr
.GetTextColour().GetAsString());
1914 msg
+= ", no colour";
1917 msg
<< ", " << alignment
;
1921 if (attr
.GetFont().GetWeight() == wxBOLD
)
1922 msg
+= wxT(" BOLD");
1923 else if (attr
.GetFont().GetWeight() == wxNORMAL
)
1924 msg
+= wxT(" NORMAL");
1926 if (attr
.GetFont().GetStyle() == wxITALIC
)
1927 msg
+= wxT(" ITALIC");
1929 if (attr
.GetFont().GetUnderlined())
1930 msg
+= wxT(" UNDERLINED");
1935 #endif // wxUSE_STATUSBAR
1936 m_currentPosition
= insertionPoint
;