1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: TextCtrl wxWidgets sample
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows license
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"
47 //----------------------------------------------------------------------
49 //----------------------------------------------------------------------
51 class MyApp
: public wxApp
57 // a text ctrl which allows to call different wxTextCtrl functions
58 // interactively by pressing function keys in it
59 class MyTextCtrl
: public wxTextCtrl
62 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
63 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
64 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
)
69 void OnKeyDown(wxKeyEvent
& event
);
70 void OnKeyUp(wxKeyEvent
& event
);
71 void OnChar(wxKeyEvent
& event
);
73 void OnText(wxCommandEvent
& event
);
74 void OnTextURL(wxTextUrlEvent
& event
);
75 void OnTextMaxLen(wxCommandEvent
& event
);
77 void OnMouseEvent(wxMouseEvent
& event
);
79 void OnSetFocus(wxFocusEvent
& event
);
80 void OnKillFocus(wxFocusEvent
& event
);
82 static bool ms_logKey
;
83 static bool ms_logChar
;
84 static bool ms_logMouse
;
85 static bool ms_logText
;
86 static bool ms_logFocus
;
89 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
90 void LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
97 class MyPanel
: public wxPanel
100 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
104 delete wxLog::SetActiveTarget(m_logOld
);
109 void DoPasteFromClipboard();
110 void DoCopyToClipboard();
111 #endif // wxUSE_CLIPBOARD
114 void DoReplaceText();
116 void DoMoveToEndOfText();
117 void DoMoveToEndOfEntry();
120 MyTextCtrl
*m_password
;
123 MyTextCtrl
*m_readonly
;
124 MyTextCtrl
*m_limited
;
126 MyTextCtrl
*m_multitext
;
127 MyTextCtrl
*m_horizontal
;
129 MyTextCtrl
*m_textrich
;
137 // get the currently focused text control or return the default one is no
138 // text ctrl has focus
139 wxTextCtrl
*GetFocusedText(wxTextCtrl
*textDef
);
142 class MyFrame
: public wxFrame
145 MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
147 void OnQuit(wxCommandEvent
& event
);
148 void OnAbout(wxCommandEvent
& event
);
150 void OnSetTooltipDelay(wxCommandEvent
& event
);
151 void OnToggleTooltips(wxCommandEvent
& event
);
152 #endif // wxUSE_TOOLTIPS
155 void OnPasteFromClipboard( wxCommandEvent
& WXUNUSED(event
) )
156 { m_panel
->DoPasteFromClipboard(); }
157 void OnCopyToClipboard( wxCommandEvent
& WXUNUSED(event
) )
158 { m_panel
->DoCopyToClipboard(); }
159 #endif // wxUSE_CLIPBOARD
161 void OnAddTextFreeze( wxCommandEvent
& WXUNUSED(event
) )
163 void OnAddText( wxCommandEvent
& WXUNUSED(event
) )
164 { DoAddText(false); }
165 void OnRemoveText( wxCommandEvent
& WXUNUSED(event
) )
166 { m_panel
->DoRemoveText(); }
167 void OnReplaceText( wxCommandEvent
& WXUNUSED(event
) )
168 { m_panel
->DoReplaceText(); }
169 void OnSelectText( wxCommandEvent
& WXUNUSED(event
) )
170 { m_panel
->DoSelectText(); }
172 void OnMoveToEndOfText( wxCommandEvent
& WXUNUSED(event
) )
173 { m_panel
->DoMoveToEndOfText(); }
174 void OnMoveToEndOfEntry( wxCommandEvent
& WXUNUSED(event
) )
175 { m_panel
->DoMoveToEndOfEntry(); }
177 void OnScrollLineDown(wxCommandEvent
& WXUNUSED(event
))
179 if ( !m_panel
->m_textrich
->LineDown() )
180 wxLogMessage(_T("Already at the bottom"));
183 void OnScrollLineUp(wxCommandEvent
& WXUNUSED(event
))
185 if ( !m_panel
->m_textrich
->LineUp() )
186 wxLogMessage(_T("Already at the top"));
189 void OnScrollPageDown(wxCommandEvent
& WXUNUSED(event
))
191 if ( !m_panel
->m_textrich
->PageDown() )
192 wxLogMessage(_T("Already at the bottom"));
195 void OnScrollPageUp(wxCommandEvent
& WXUNUSED(event
))
197 if ( !m_panel
->m_textrich
->PageUp() )
198 wxLogMessage(_T("Already at the top"));
201 void OnGetLine(wxCommandEvent
& WXUNUSED(event
))
203 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
204 wxT("Enter which line you would like to get"),
205 wxT("Get a line from the tabbed multiline text control") );
207 wxMessageBox(m_panel
->m_tab
->GetLineText(nLine
));
210 void OnGetLineLength(wxCommandEvent
& WXUNUSED(event
))
212 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
213 wxT("Enter which line you would like to get"),
214 wxT("Get length of a line from the tabbed multiline text control") );
216 wxMessageBox(wxString::Format(wxT("Length of line %i is:%i"),
218 m_panel
->m_tab
->GetLineLength(nLine
))
223 void OnLogClear(wxCommandEvent
& event
);
225 void OnFileSave(wxCommandEvent
& event
);
226 void OnFileLoad(wxCommandEvent
& event
);
227 void OnRichTextTest(wxCommandEvent
& event
);
229 void OnSetEditable(wxCommandEvent
& event
);
230 void OnSetEnabled(wxCommandEvent
& event
);
232 void OnLogKey(wxCommandEvent
& event
)
234 MyTextCtrl::ms_logKey
= event
.IsChecked();
237 void OnLogChar(wxCommandEvent
& event
)
239 MyTextCtrl::ms_logChar
= event
.IsChecked();
242 void OnLogMouse(wxCommandEvent
& event
)
244 MyTextCtrl::ms_logMouse
= event
.IsChecked();
247 void OnLogText(wxCommandEvent
& event
)
249 MyTextCtrl::ms_logText
= event
.IsChecked();
252 void OnLogFocus(wxCommandEvent
& event
)
254 MyTextCtrl::ms_logFocus
= event
.IsChecked();
257 void OnSetText(wxCommandEvent
& WXUNUSED(event
))
259 m_panel
->m_text
->SetValue(_T("Hello, world (what else did you expect)?"));
262 void OnIdle( wxIdleEvent
& event
);
265 void DoAddText(bool freeze
)
267 wxTextCtrl
* const text
= m_panel
->m_textrich
;
273 for ( int i
= 0; i
< 100; i
++ )
275 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
281 text
->SetInsertionPoint(0);
286 DECLARE_EVENT_TABLE()
290 * RichTextFrame is used to demonstrate rich text behaviour
293 class RichTextFrame
: public wxFrame
296 RichTextFrame(wxWindow
* parent
, const wxString
& title
);
300 void OnClose(wxCommandEvent
& event
);
301 void OnIdle(wxIdleEvent
& event
);
302 void OnLeftAlign(wxCommandEvent
& event
);
303 void OnRightAlign(wxCommandEvent
& event
);
304 void OnJustify(wxCommandEvent
& event
);
305 void OnCentre(wxCommandEvent
& event
);
306 void OnChangeFont(wxCommandEvent
& event
);
307 void OnChangeTextColour(wxCommandEvent
& event
);
308 void OnChangeBackgroundColour(wxCommandEvent
& event
);
309 void OnLeftIndent(wxCommandEvent
& event
);
310 void OnRightIndent(wxCommandEvent
& event
);
313 wxTextCtrl
*m_textCtrl
;
314 long m_currentPosition
;
316 DECLARE_EVENT_TABLE()
319 //----------------------------------------------------------------------
321 //----------------------------------------------------------------------
325 //----------------------------------------------------------------------
327 //----------------------------------------------------------------------
339 TEXT_CLIPBOARD_COPY
= 200,
340 TEXT_CLIPBOARD_PASTE
,
343 TEXT_TOOLTIPS_SETDELAY
= 300,
344 TEXT_TOOLTIPS_ENABLE
,
378 // Create the main frame window
379 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
380 _T("Text wxWidgets sample"), 50, 50, 700, 420);
381 frame
->SetSizeHints( 500, 400 );
383 wxMenu
*file_menu
= new wxMenu
;
384 file_menu
->Append(TEXT_SAVE
, _T("&Save file\tCtrl-S"),
385 _T("Save the text control contents to file"));
386 file_menu
->Append(TEXT_LOAD
, _T("&Load file\tCtrl-O"),
387 _T("Load the sample file into text control"));
388 file_menu
->AppendSeparator();
389 file_menu
->Append(TEXT_RICH_TEXT_TEST
, _T("Show Rich Text Editor"));
390 file_menu
->AppendSeparator();
391 file_menu
->Append(TEXT_ABOUT
, _T("&About\tAlt-A"));
392 file_menu
->AppendSeparator();
393 file_menu
->Append(TEXT_QUIT
, _T("E&xit\tAlt-X"), _T("Quit this sample"));
395 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
396 menu_bar
->Append(file_menu
, _T("&File"));
399 wxMenu
*tooltip_menu
= new wxMenu
;
400 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, _T("Set &delay\tCtrl-D"));
401 tooltip_menu
->AppendSeparator();
402 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, _T("&Toggle tooltips\tCtrl-T"),
403 _T("enable/disable tooltips"), true);
404 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, true);
405 menu_bar
->Append(tooltip_menu
, _T("&Tooltips"));
406 #endif // wxUSE_TOOLTIPS
409 wxMenu
*menuClipboard
= new wxMenu
;
410 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, _T("&Copy\tCtrl-C"),
411 _T("Copy the first line to the clipboard"));
412 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, _T("&Paste\tCtrl-V"),
413 _T("Paste from clipboard to the text control"));
414 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
415 #endif // wxUSE_CLIPBOARD
417 wxMenu
*menuText
= new wxMenu
;
418 menuText
->Append(TEXT_ADD_SOME
, _T("&Append some text\tCtrl-A"));
419 menuText
->Append(TEXT_ADD_FREEZE
, _T("&Append text with freeze/thaw\tShift-Ctrl-A"));
420 menuText
->Append(TEXT_REMOVE
, _T("&Remove first 10 characters\tCtrl-Y"));
421 menuText
->Append(TEXT_REPLACE
, _T("&Replace characters 4 to 8 with ABC\tCtrl-R"));
422 menuText
->Append(TEXT_SELECT
, _T("&Select characters 4 to 8\tCtrl-I"));
423 menuText
->Append(TEXT_SET
, _T("&Set the first text zone value\tCtrl-E"));
424 menuText
->AppendSeparator();
425 menuText
->Append(TEXT_MOVE_ENDTEXT
, _T("Move cursor to the end of &text"));
426 menuText
->Append(TEXT_MOVE_ENDENTRY
, _T("Move cursor to the end of &entry"));
427 menuText
->AppendCheckItem(TEXT_SET_EDITABLE
, _T("Toggle &editable state"));
428 menuText
->AppendCheckItem(TEXT_SET_ENABLED
, _T("Toggle e&nabled state"));
429 menuText
->Check(TEXT_SET_EDITABLE
, true);
430 menuText
->Check(TEXT_SET_ENABLED
, true);
431 menuText
->AppendSeparator();
432 menuText
->Append(TEXT_LINE_DOWN
, _T("Scroll text one line down"));
433 menuText
->Append(TEXT_LINE_UP
, _T("Scroll text one line up"));
434 menuText
->Append(TEXT_PAGE_DOWN
, _T("Scroll text one page down"));
435 menuText
->Append(TEXT_PAGE_UP
, _T("Scroll text one page up"));
436 menuText
->AppendSeparator();
437 menuText
->Append(TEXT_GET_LINE
, _T("Get the text of a line of the tabbed multiline"));
438 menuText
->Append(TEXT_GET_LINELENGTH
, _T("Get the length of a line of the tabbed multiline"));
439 menu_bar
->Append(menuText
, _T("Te&xt"));
442 wxMenu
*menuLog
= new wxMenu
;
443 menuLog
->AppendCheckItem(TEXT_LOG_KEY
, _T("Log &key events"));
444 menuLog
->AppendCheckItem(TEXT_LOG_CHAR
, _T("Log &char events"));
445 menuLog
->AppendCheckItem(TEXT_LOG_MOUSE
, _T("Log &mouse events"));
446 menuLog
->AppendCheckItem(TEXT_LOG_TEXT
, _T("Log &text events"));
447 menuLog
->AppendCheckItem(TEXT_LOG_FOCUS
, _T("Log &focus events"));
448 menuLog
->AppendSeparator();
449 menuLog
->Append(TEXT_CLEAR
, _T("&Clear the log\tCtrl-L"),
450 _T("Clear the log window contents"));
452 // select only the interesting events by default
453 MyTextCtrl::ms_logKey
=
454 MyTextCtrl::ms_logChar
= false;
455 MyTextCtrl::ms_logText
= true;
457 menuLog
->Check(TEXT_LOG_KEY
, MyTextCtrl::ms_logKey
);
458 menuLog
->Check(TEXT_LOG_CHAR
, MyTextCtrl::ms_logChar
);
459 menuLog
->Check(TEXT_LOG_TEXT
, MyTextCtrl::ms_logText
);
461 menu_bar
->Append(menuLog
, _T("&Log"));
464 frame
->SetMenuBar(menu_bar
);
474 //----------------------------------------------------------------------
476 //----------------------------------------------------------------------
478 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
479 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
480 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
481 EVT_CHAR(MyTextCtrl::OnChar
)
483 EVT_TEXT(wxID_ANY
, MyTextCtrl::OnText
)
484 EVT_TEXT_URL(wxID_ANY
, MyTextCtrl::OnTextURL
)
485 EVT_TEXT_MAXLEN(wxID_ANY
, MyTextCtrl::OnTextMaxLen
)
487 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
489 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus
)
490 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus
)
493 bool MyTextCtrl::ms_logKey
= false;
494 bool MyTextCtrl::ms_logChar
= false;
495 bool MyTextCtrl::ms_logMouse
= false;
496 bool MyTextCtrl::ms_logText
= false;
497 bool MyTextCtrl::ms_logFocus
= false;
499 void MyTextCtrl::LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const
502 long keycode
= event
.GetKeyCode();
506 case WXK_BACK
: key
= _T("BACK"); break;
507 case WXK_TAB
: key
= _T("TAB"); break;
508 case WXK_RETURN
: key
= _T("RETURN"); break;
509 case WXK_ESCAPE
: key
= _T("ESCAPE"); break;
510 case WXK_SPACE
: key
= _T("SPACE"); break;
511 case WXK_DELETE
: key
= _T("DELETE"); break;
512 case WXK_START
: key
= _T("START"); break;
513 case WXK_LBUTTON
: key
= _T("LBUTTON"); break;
514 case WXK_RBUTTON
: key
= _T("RBUTTON"); break;
515 case WXK_CANCEL
: key
= _T("CANCEL"); break;
516 case WXK_MBUTTON
: key
= _T("MBUTTON"); break;
517 case WXK_CLEAR
: key
= _T("CLEAR"); break;
518 case WXK_SHIFT
: key
= _T("SHIFT"); break;
519 case WXK_ALT
: key
= _T("ALT"); break;
520 case WXK_CONTROL
: key
= _T("CONTROL"); break;
521 case WXK_MENU
: key
= _T("MENU"); break;
522 case WXK_PAUSE
: key
= _T("PAUSE"); break;
523 case WXK_CAPITAL
: key
= _T("CAPITAL"); break;
524 case WXK_PRIOR
: key
= _T("PRIOR"); break;
525 case WXK_NEXT
: key
= _T("NEXT"); break;
526 case WXK_END
: key
= _T("END"); break;
527 case WXK_HOME
: key
= _T("HOME"); break;
528 case WXK_LEFT
: key
= _T("LEFT"); break;
529 case WXK_UP
: key
= _T("UP"); break;
530 case WXK_RIGHT
: key
= _T("RIGHT"); break;
531 case WXK_DOWN
: key
= _T("DOWN"); break;
532 case WXK_SELECT
: key
= _T("SELECT"); break;
533 case WXK_PRINT
: key
= _T("PRINT"); break;
534 case WXK_EXECUTE
: key
= _T("EXECUTE"); break;
535 case WXK_SNAPSHOT
: key
= _T("SNAPSHOT"); break;
536 case WXK_INSERT
: key
= _T("INSERT"); break;
537 case WXK_HELP
: key
= _T("HELP"); break;
538 case WXK_NUMPAD0
: key
= _T("NUMPAD0"); break;
539 case WXK_NUMPAD1
: key
= _T("NUMPAD1"); break;
540 case WXK_NUMPAD2
: key
= _T("NUMPAD2"); break;
541 case WXK_NUMPAD3
: key
= _T("NUMPAD3"); break;
542 case WXK_NUMPAD4
: key
= _T("NUMPAD4"); break;
543 case WXK_NUMPAD5
: key
= _T("NUMPAD5"); break;
544 case WXK_NUMPAD6
: key
= _T("NUMPAD6"); break;
545 case WXK_NUMPAD7
: key
= _T("NUMPAD7"); break;
546 case WXK_NUMPAD8
: key
= _T("NUMPAD8"); break;
547 case WXK_NUMPAD9
: key
= _T("NUMPAD9"); break;
548 case WXK_MULTIPLY
: key
= _T("MULTIPLY"); break;
549 case WXK_ADD
: key
= _T("ADD"); break;
550 case WXK_SEPARATOR
: key
= _T("SEPARATOR"); break;
551 case WXK_SUBTRACT
: key
= _T("SUBTRACT"); break;
552 case WXK_DECIMAL
: key
= _T("DECIMAL"); break;
553 case WXK_DIVIDE
: key
= _T("DIVIDE"); break;
554 case WXK_F1
: key
= _T("F1"); break;
555 case WXK_F2
: key
= _T("F2"); break;
556 case WXK_F3
: key
= _T("F3"); break;
557 case WXK_F4
: key
= _T("F4"); break;
558 case WXK_F5
: key
= _T("F5"); break;
559 case WXK_F6
: key
= _T("F6"); break;
560 case WXK_F7
: key
= _T("F7"); break;
561 case WXK_F8
: key
= _T("F8"); break;
562 case WXK_F9
: key
= _T("F9"); break;
563 case WXK_F10
: key
= _T("F10"); break;
564 case WXK_F11
: key
= _T("F11"); break;
565 case WXK_F12
: key
= _T("F12"); break;
566 case WXK_F13
: key
= _T("F13"); break;
567 case WXK_F14
: key
= _T("F14"); break;
568 case WXK_F15
: key
= _T("F15"); break;
569 case WXK_F16
: key
= _T("F16"); break;
570 case WXK_F17
: key
= _T("F17"); break;
571 case WXK_F18
: key
= _T("F18"); break;
572 case WXK_F19
: key
= _T("F19"); break;
573 case WXK_F20
: key
= _T("F20"); break;
574 case WXK_F21
: key
= _T("F21"); break;
575 case WXK_F22
: key
= _T("F22"); break;
576 case WXK_F23
: key
= _T("F23"); break;
577 case WXK_F24
: key
= _T("F24"); break;
578 case WXK_NUMLOCK
: key
= _T("NUMLOCK"); break;
579 case WXK_SCROLL
: key
= _T("SCROLL"); break;
580 case WXK_PAGEUP
: key
= _T("PAGEUP"); break;
581 case WXK_PAGEDOWN
: key
= _T("PAGEDOWN"); break;
582 case WXK_NUMPAD_SPACE
: key
= _T("NUMPAD_SPACE"); break;
583 case WXK_NUMPAD_TAB
: key
= _T("NUMPAD_TAB"); break;
584 case WXK_NUMPAD_ENTER
: key
= _T("NUMPAD_ENTER"); break;
585 case WXK_NUMPAD_F1
: key
= _T("NUMPAD_F1"); break;
586 case WXK_NUMPAD_F2
: key
= _T("NUMPAD_F2"); break;
587 case WXK_NUMPAD_F3
: key
= _T("NUMPAD_F3"); break;
588 case WXK_NUMPAD_F4
: key
= _T("NUMPAD_F4"); break;
589 case WXK_NUMPAD_HOME
: key
= _T("NUMPAD_HOME"); break;
590 case WXK_NUMPAD_LEFT
: key
= _T("NUMPAD_LEFT"); break;
591 case WXK_NUMPAD_UP
: key
= _T("NUMPAD_UP"); break;
592 case WXK_NUMPAD_RIGHT
: key
= _T("NUMPAD_RIGHT"); break;
593 case WXK_NUMPAD_DOWN
: key
= _T("NUMPAD_DOWN"); break;
594 case WXK_NUMPAD_PRIOR
: key
= _T("NUMPAD_PRIOR"); break;
595 case WXK_NUMPAD_PAGEUP
: key
= _T("NUMPAD_PAGEUP"); break;
596 case WXK_NUMPAD_PAGEDOWN
: key
= _T("NUMPAD_PAGEDOWN"); break;
597 case WXK_NUMPAD_END
: key
= _T("NUMPAD_END"); break;
598 case WXK_NUMPAD_BEGIN
: key
= _T("NUMPAD_BEGIN"); break;
599 case WXK_NUMPAD_INSERT
: key
= _T("NUMPAD_INSERT"); break;
600 case WXK_NUMPAD_DELETE
: key
= _T("NUMPAD_DELETE"); break;
601 case WXK_NUMPAD_EQUAL
: key
= _T("NUMPAD_EQUAL"); break;
602 case WXK_NUMPAD_MULTIPLY
: key
= _T("NUMPAD_MULTIPLY"); break;
603 case WXK_NUMPAD_ADD
: key
= _T("NUMPAD_ADD"); break;
604 case WXK_NUMPAD_SEPARATOR
: key
= _T("NUMPAD_SEPARATOR"); break;
605 case WXK_NUMPAD_SUBTRACT
: key
= _T("NUMPAD_SUBTRACT"); break;
606 case WXK_NUMPAD_DECIMAL
: key
= _T("NUMPAD_DECIMAL"); break;
610 if ( wxIsprint((int)keycode
) )
611 key
.Printf(_T("'%c'"), (char)keycode
);
612 else if ( keycode
> 0 && keycode
< 27 )
613 key
.Printf(_("Ctrl-%c"), _T('A') + keycode
- 1);
615 key
.Printf(_T("unknown (%ld)"), keycode
);
621 key
+= wxString::Format(_T(" (Unicode: %#04x)"), event
.GetUnicodeKey());
622 #endif // wxUSE_UNICODE
624 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
627 GetChar( event
.ControlDown(), _T('C') ),
628 GetChar( event
.AltDown(), _T('A') ),
629 GetChar( event
.ShiftDown(), _T('S') ),
630 GetChar( event
.MetaDown(), _T('M') ) );
633 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
638 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
641 dbl
= ev
.LeftDClick();
644 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
646 button
= _T("Middle");
647 dbl
= ev
.MiddleDClick();
650 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
652 button
= _T("Right");
653 dbl
= ev
.RightDClick();
658 return _T("Unknown mouse event");
661 return wxString::Format(_T("%s mouse button %s"),
663 dbl
? _T("double clicked")
664 : up
? _T("released") : _T("clicked"));
667 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
679 msg
= _T("Mouse entered the window");
681 else if ( ev
.Leaving() )
683 msg
= _T("Mouse left the window");
687 msg
= GetMouseEventDesc(ev
);
690 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ");
693 wxTextCtrlHitTestResult rc
= HitTest(ev
.GetPosition(), &pos
);
694 if ( rc
!= wxTE_HT_UNKNOWN
)
696 msg
<< _T("at position ") << pos
<< _T(' ');
699 msg
<< _T("[Flags: ")
700 << GetChar( ev
.LeftIsDown(), _T('1') )
701 << GetChar( ev
.MiddleIsDown(), _T('2') )
702 << GetChar( ev
.RightIsDown(), _T('3') )
703 << GetChar( ev
.ControlDown(), _T('C') )
704 << GetChar( ev
.AltDown(), _T('A') )
705 << GetChar( ev
.ShiftDown(), _T('S') )
706 << GetChar( ev
.MetaDown(), _T('M') )
711 //else: we're not interested in mouse move events
714 void MyTextCtrl::OnSetFocus(wxFocusEvent
& event
)
717 wxLogMessage( wxT("%p got focus."), this);
722 void MyTextCtrl::OnKillFocus(wxFocusEvent
& event
)
725 wxLogMessage( wxT("%p lost focus"), this);
730 void MyTextCtrl::OnText(wxCommandEvent
& event
)
735 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
736 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
739 wxLogMessage(_T("Text changed in control '%s'"), data
);
743 // wxLogMessage( event.GetString() );
744 wxLogMessage(_T("Text changed in some control"));
748 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& WXUNUSED(event
))
750 wxLogMessage(_T("You can't enter more characters into this control."));
753 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
755 const wxMouseEvent
& ev
= event
.GetMouseEvent();
757 // filter out mouse moves, too many of them
761 long start
= event
.GetURLStart(),
762 end
= event
.GetURLEnd();
764 wxLogMessage(_T("Mouse event over URL '%s': %s"),
765 GetValue().Mid(start
, end
- start
).c_str(),
766 GetMouseEventDesc(ev
).c_str());
769 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
772 LogKeyEvent( _T("Char"), event
);
777 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
780 LogKeyEvent( _T("Key up"), event
);
785 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
787 switch ( event
.GetKeyCode() )
790 // show current position and text length
792 long line
, column
, pos
= GetInsertionPoint();
793 PositionToXY(pos
, &column
, &line
);
795 wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
798 (long) GetNumberOfLines(),
799 (long) GetLineLength(line
),
800 (unsigned int) GetValue().length(),
804 GetSelection(&from
, &to
);
806 wxString sel
= GetStringSelection();
808 wxLogMessage(_T("Selection: from %ld to %ld."), from
, to
);
809 wxLogMessage(_T("Selection = '%s' (len = %u)"),
811 (unsigned int) sel
.length());
817 SetInsertionPointEnd();
822 SetInsertionPoint(10);
828 wxLogDebug( wxT("Now capturing mouse and events.") );
834 wxLogDebug( wxT("Stopped capturing mouse and events.") );
835 m_hasCapture
= false;
841 // insert a blank line
846 wxLogMessage(_T("IsModified() before SetValue(): %d"),
848 SetValue(_T("SetValue() has been called"));
849 wxLogMessage(_T("IsModified() after SetValue(): %d"),
854 wxLogMessage(_T("Position 10 should be now visible."));
859 wxLogMessage(_T("Control has been cleared"));
864 WriteText(_T("WriteText() has been called"));
868 AppendText(_T("AppendText() has been called"));
873 LogKeyEvent( wxT("Key down"), event
);
878 //----------------------------------------------------------------------
880 //----------------------------------------------------------------------
882 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
883 : wxPanel( frame
, wxID_ANY
, wxPoint(x
, y
), wxSize(w
, h
) )
886 m_log
= new wxTextCtrl( this, wxID_ANY
, _T("This is the log window.\n"),
887 wxPoint(5,260), wxSize(630,100),
888 wxTE_MULTILINE
| wxTE_READONLY
/* | wxTE_RICH */);
890 m_logOld
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
893 // single line text controls
895 m_text
= new MyTextCtrl( this, wxID_ANY
, _T("Single line."),
896 wxDefaultPosition
, wxDefaultSize
,
898 m_text
->SetForegroundColour(*wxBLUE
);
899 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
900 (*m_text
) << _T(" Appended.");
901 m_text
->SetInsertionPoint(0);
902 m_text
->WriteText( _T("Prepended. ") );
904 m_password
= new MyTextCtrl( this, wxID_ANY
, _T(""),
905 wxPoint(10,50), wxSize(140,wxDefaultCoord
), wxTE_PASSWORD
);
907 m_readonly
= new MyTextCtrl( this, wxID_ANY
, _T("Read only"),
908 wxPoint(10,90), wxSize(140,wxDefaultCoord
), wxTE_READONLY
);
910 m_limited
= new MyTextCtrl(this, wxID_ANY
, _T("Max 8 ch"),
911 wxPoint(10, 130), wxSize(140, wxDefaultCoord
));
912 m_limited
->SetMaxLength(8);
914 // multi line text controls
916 m_horizontal
= new MyTextCtrl( this, wxID_ANY
, _T("Multiline text control with a horizontal scrollbar.\n"),
917 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
919 // a little hack to use the command line argument for encoding testing
920 if ( wxTheApp
->argc
== 2 )
922 switch ( wxTheApp
->argv
[1][0] )
925 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
927 wxFONTENCODING_ISO8859_2
));
928 m_horizontal
->AppendText(_T("®lu»ouèký kùò zbìsile èe¹tina «»"));
932 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
934 wxFONTENCODING_CP1251
));
935 m_horizontal
->AppendText(_T("Ïðèâåò!"));
939 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
941 wxFONTENCODING_CP1251
));
943 m_horizontal
->AppendText(L
"\x0412\x0430\x0434\x0438\x043c \x0426");
945 m_horizontal
->AppendText("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
951 m_horizontal
->AppendText(_T("Text in default encoding"));
954 m_multitext
= new MyTextCtrl( this, wxID_ANY
, _T("Multi line."),
955 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
956 m_multitext
->SetFont(*wxITALIC_FONT
);
957 (*m_multitext
) << _T(" Appended.");
958 m_multitext
->SetInsertionPoint(0);
959 m_multitext
->WriteText( _T("Prepended. ") );
960 m_multitext
->SetForegroundColour(*wxRED
);
961 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
964 m_multitext
->SetToolTip(_T("Press Fn function keys here"));
967 m_tab
= new MyTextCtrl( this, 100, _T("Multiline, allow <TAB> processing."),
968 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
969 m_tab
->SetClientData((void *)_T("tab"));
971 m_enter
= new MyTextCtrl( this, 100, _T("Multiline, allow <ENTER> processing."),
972 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
973 m_enter
->SetClientData((void *)_T("enter"));
975 m_textrich
= new MyTextCtrl(this, wxID_ANY
, _T("Allows more than 30Kb of text\n")
976 _T("(even under broken Win9x)\n")
977 _T("and a very very very very very ")
978 _T("very very very long line to test ")
979 _T("wxHSCROLL style"),
980 wxPoint(450, 10), wxSize(230, 230),
981 wxTE_RICH
| wxTE_MULTILINE
);
982 m_textrich
->SetStyle(0, 10, *wxRED
);
983 m_textrich
->SetStyle(10, 20, *wxBLUE
);
984 m_textrich
->SetStyle(30, 40,
985 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
986 m_textrich
->SetDefaultStyle(wxTextAttr());
987 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
988 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
989 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
990 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
991 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
992 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
993 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
, *wxWHITE
));
994 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
995 _T("type should be in blue as well"));
998 // lay out the controls
999 wxBoxSizer
*column1
= new wxBoxSizer(wxVERTICAL
);
1000 column1
->Add( m_text
, 0, wxALL
| wxEXPAND
, 10 );
1001 column1
->Add( m_password
, 0, wxALL
| wxEXPAND
, 10 );
1002 column1
->Add( m_readonly
, 0, wxALL
| wxEXPAND
, 10 );
1003 column1
->Add( m_limited
, 0, wxALL
| wxEXPAND
, 10 );
1004 column1
->Add( m_horizontal
, 1, wxALL
| wxEXPAND
, 10 );
1006 wxBoxSizer
*column2
= new wxBoxSizer(wxVERTICAL
);
1007 column2
->Add( m_multitext
, 1, wxALL
| wxEXPAND
, 10 );
1008 column2
->Add( m_tab
, 1, wxALL
| wxEXPAND
, 10 );
1009 column2
->Add( m_enter
, 1, wxALL
| wxEXPAND
, 10 );
1011 wxBoxSizer
*row1
= new wxBoxSizer(wxHORIZONTAL
);
1012 row1
->Add( column1
, 0, wxALL
| wxEXPAND
, 10 );
1013 row1
->Add( column2
, 1, wxALL
| wxEXPAND
, 10 );
1014 row1
->Add( m_textrich
, 1, wxALL
| wxEXPAND
, 10 );
1016 wxBoxSizer
*topSizer
= new wxBoxSizer(wxVERTICAL
);
1017 topSizer
->Add( row1
, 2, wxALL
| wxEXPAND
, 10 );
1020 topSizer
->Add( m_log
, 1, wxALL
| wxEXPAND
, 10 );
1023 SetAutoLayout( true );
1027 wxTextCtrl
*MyPanel::GetFocusedText(wxTextCtrl
*textDef
)
1029 wxWindow
*win
= FindFocus();
1031 wxTextCtrl
*text
= win
? wxDynamicCast(win
, wxTextCtrl
) : NULL
;
1032 return text
? text
: textDef
;
1036 void MyPanel::DoPasteFromClipboard()
1038 // On X11, we want to get the data from the primary selection instead
1039 // of the normal clipboard (which isn't normal under X11 at all). This
1040 // call has no effect under MSW.
1041 wxTheClipboard
->UsePrimarySelection();
1043 if (!wxTheClipboard
->Open())
1046 *m_log
<< _T("Error opening the clipboard.\n");
1053 *m_log
<< _T("Successfully opened the clipboard.\n");
1057 wxTextDataObject data
;
1059 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
1062 *m_log
<< _T("Clipboard supports requested format.\n");
1065 if (wxTheClipboard
->GetData( data
))
1068 *m_log
<< _T("Successfully retrieved data from the clipboard.\n");
1070 *m_multitext
<< data
.GetText() << _T("\n");
1075 *m_log
<< _T("Error getting data from the clipboard.\n");
1082 *m_log
<< _T("Clipboard doesn't support requested format.\n");
1086 wxTheClipboard
->Close();
1089 *m_log
<< _T("Closed the clipboard.\n");
1093 void MyPanel::DoCopyToClipboard()
1095 // On X11, we want to get the data from the primary selection instead
1096 // of the normal clipboard (which isn't normal under X11 at all). This
1097 // call has no effect under MSW.
1098 wxTheClipboard
->UsePrimarySelection();
1100 wxString
text( m_multitext
->GetLineText(0) );
1105 *m_log
<< _T("No text to copy.\n");
1111 if (!wxTheClipboard
->Open())
1114 *m_log
<< _T("Error opening the clipboard.\n");
1122 *m_log
<< _T("Successfully opened the clipboard.\n");
1126 wxTextDataObject
*data
= new wxTextDataObject( text
);
1128 if (!wxTheClipboard
->SetData( data
))
1131 *m_log
<< _T("Error while copying to the clipboard.\n");
1137 *m_log
<< _T("Successfully copied data to the clipboard.\n");
1141 wxTheClipboard
->Close();
1144 *m_log
<< _T("Closed the clipboard.\n");
1148 #endif // wxUSE_CLIPBOARD
1150 void MyPanel::DoMoveToEndOfText()
1152 m_multitext
->SetInsertionPointEnd();
1153 m_multitext
->SetFocus();
1156 void MyPanel::DoMoveToEndOfEntry()
1158 m_text
->SetInsertionPointEnd();
1162 void MyPanel::DoRemoveText()
1164 GetFocusedText(m_multitext
)->Remove(0, 10);
1167 void MyPanel::DoReplaceText()
1169 GetFocusedText(m_multitext
)->Replace(3, 8, _T("ABC"));
1172 void MyPanel::DoSelectText()
1174 GetFocusedText(m_multitext
)->SetSelection(3, 8);
1177 //----------------------------------------------------------------------
1179 //----------------------------------------------------------------------
1181 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1182 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
1183 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
1184 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
1185 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
1186 EVT_MENU(TEXT_RICH_TEXT_TEST
, MyFrame::OnRichTextTest
)
1188 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
1189 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
1190 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
1191 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
1192 EVT_MENU(TEXT_LOG_FOCUS
,MyFrame::OnLogFocus
)
1194 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
1198 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
1199 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
1200 #endif // wxUSE_TOOLTIPS
1203 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
1204 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
1205 #endif // wxUSE_CLIPBOARD
1207 EVT_MENU(TEXT_REMOVE
, MyFrame::OnRemoveText
)
1208 EVT_MENU(TEXT_REPLACE
, MyFrame::OnReplaceText
)
1209 EVT_MENU(TEXT_SELECT
, MyFrame::OnSelectText
)
1210 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
1211 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
1212 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
1213 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
1215 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
1216 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
1218 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
1219 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
1220 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
1221 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
1223 EVT_MENU(TEXT_GET_LINE
, MyFrame::OnGetLine
)
1224 EVT_MENU(TEXT_GET_LINELENGTH
, MyFrame::OnGetLineLength
)
1226 EVT_MENU(TEXT_SET
, MyFrame::OnSetText
)
1228 EVT_IDLE(MyFrame::OnIdle
)
1231 MyFrame::MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
)
1232 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
) )
1236 #endif // wxUSE_STATUSBAR
1238 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
1241 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
1246 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1248 wxBeginBusyCursor();
1250 wxMessageDialog
dialog(this,
1251 _T("This is a text control sample. It demonstrates the many different\n")
1252 _T("text control styles, the use of the clipboard, setting and handling\n")
1253 _T("tooltips and intercepting key and char events.\n")
1255 _T("Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin"),
1256 _T("About wxTextCtrl Sample"),
1257 wxOK
| wxICON_INFORMATION
);
1266 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& WXUNUSED(event
))
1268 static long s_delay
= 5000;
1271 delay
.Printf( _T("%ld"), s_delay
);
1273 delay
= wxGetTextFromUser(_T("Enter delay (in milliseconds)"),
1274 _T("Set tooltip delay"),
1278 return; // cancelled
1280 wxSscanf(delay
, _T("%ld"), &s_delay
);
1282 wxToolTip::SetDelay(s_delay
);
1284 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
1287 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
1289 static bool s_enabled
= true;
1291 s_enabled
= !s_enabled
;
1293 wxToolTip::Enable(s_enabled
);
1295 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
1300 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1302 m_panel
->m_log
->Clear();
1306 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1308 static bool s_editable
= true;
1310 s_editable
= !s_editable
;
1311 m_panel
->m_text
->SetEditable(s_editable
);
1312 m_panel
->m_password
->SetEditable(s_editable
);
1313 m_panel
->m_multitext
->SetEditable(s_editable
);
1314 m_panel
->m_textrich
->SetEditable(s_editable
);
1317 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1319 bool enabled
= m_panel
->m_text
->IsEnabled();
1322 m_panel
->m_text
->Enable(enabled
);
1323 m_panel
->m_password
->Enable(enabled
);
1324 m_panel
->m_multitext
->Enable(enabled
);
1325 m_panel
->m_readonly
->Enable(enabled
);
1326 m_panel
->m_limited
->Enable(enabled
);
1327 m_panel
->m_textrich
->Enable(enabled
);
1330 void MyFrame::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1332 if ( m_panel
->m_textrich
->SaveFile(_T("dummy.txt")) )
1335 // verify that the fil length is correct (it wasn't under Win95)
1336 wxFile
file(wxT("dummy.txt"));
1338 _T("Successfully saved file (text len = %lu, file size = %ld)"),
1339 (unsigned long)m_panel
->m_textrich
->GetValue().length(),
1340 (long) file
.Length());
1344 wxLogStatus(this, _T("Couldn't save the file"));
1347 void MyFrame::OnFileLoad(wxCommandEvent
& WXUNUSED(event
))
1349 if ( m_panel
->m_textrich
->LoadFile(_T("dummy.txt")) )
1350 wxLogStatus(this, _T("Successfully loaded file"));
1352 wxLogStatus(this, _T("Couldn't load the file"));
1355 void MyFrame::OnRichTextTest(wxCommandEvent
& WXUNUSED(event
))
1357 RichTextFrame
* frame
= new RichTextFrame(this, _T("Rich Text Editor"));
1361 void MyFrame::OnIdle( wxIdleEvent
& event
)
1363 // track the window which has the focus in the status bar
1364 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1365 wxWindow
*focus
= wxWindow::FindFocus();
1366 if ( focus
&& (focus
!= s_windowFocus
) )
1368 s_windowFocus
= focus
;
1373 _T("Focus: wxWindow = %p, HWND = %08x"),
1375 _T("Focus: wxWindow = %p"),
1379 , (unsigned int) s_windowFocus
->GetHWND()
1385 #endif // wxUSE_STATUSBAR
1391 * RichTextFrame is used to demonstrate rich text behaviour
1396 RICHTEXT_CLOSE
= 1000,
1397 RICHTEXT_LEFT_ALIGN
,
1398 RICHTEXT_RIGHT_ALIGN
,
1401 RICHTEXT_CHANGE_FONT
,
1402 RICHTEXT_CHANGE_TEXT_COLOUR
,
1403 RICHTEXT_CHANGE_BACKGROUND_COLOUR
,
1404 RICHTEXT_LEFT_INDENT
,
1405 RICHTEXT_RIGHT_INDENT
1408 BEGIN_EVENT_TABLE(RichTextFrame
, wxFrame
)
1409 EVT_IDLE(RichTextFrame::OnIdle
)
1410 EVT_MENU(RICHTEXT_CLOSE
, RichTextFrame::OnClose
)
1411 EVT_MENU(RICHTEXT_LEFT_ALIGN
, RichTextFrame::OnLeftAlign
)
1412 EVT_MENU(RICHTEXT_RIGHT_ALIGN
, RichTextFrame::OnRightAlign
)
1413 EVT_MENU(RICHTEXT_CENTRE
, RichTextFrame::OnCentre
)
1414 EVT_MENU(RICHTEXT_JUSTIFY
, RichTextFrame::OnJustify
)
1415 EVT_MENU(RICHTEXT_CHANGE_FONT
, RichTextFrame::OnChangeFont
)
1416 EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR
, RichTextFrame::OnChangeTextColour
)
1417 EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, RichTextFrame::OnChangeBackgroundColour
)
1418 EVT_MENU(RICHTEXT_LEFT_INDENT
, RichTextFrame::OnLeftIndent
)
1419 EVT_MENU(RICHTEXT_RIGHT_INDENT
, RichTextFrame::OnRightIndent
)
1422 RichTextFrame::RichTextFrame(wxWindow
* parent
, const wxString
& title
):
1423 wxFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(300, 400))
1425 m_currentPosition
= -1;
1426 m_textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1427 wxDefaultSize
, wxTE_MULTILINE
|wxTE_RICH2
);
1431 for (i
= 0; i
< 10; i
++)
1434 for (j
= 0; j
< 10; j
++)
1436 value
<< wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
1438 value
<< wxT("\n\n");
1440 m_textCtrl
->SetValue(value
);
1442 wxMenuBar
* menuBar
= new wxMenuBar
;
1443 wxMenu
* fileMenu
= new wxMenu
;
1444 fileMenu
->Append(RICHTEXT_CLOSE
, _("Close\tCtrl+W"));
1445 menuBar
->Append(fileMenu
, _("File"));
1447 wxMenu
* editMenu
= new wxMenu
;
1448 editMenu
->Append(RICHTEXT_LEFT_ALIGN
, _("Left Align"));
1449 editMenu
->Append(RICHTEXT_RIGHT_ALIGN
, _("Right Align"));
1450 editMenu
->Append(RICHTEXT_CENTRE
, _("Centre"));
1451 editMenu
->Append(RICHTEXT_JUSTIFY
, _("Justify"));
1452 editMenu
->AppendSeparator();
1453 editMenu
->Append(RICHTEXT_CHANGE_FONT
, _("Change Font"));
1454 editMenu
->Append(RICHTEXT_CHANGE_TEXT_COLOUR
, _("Change Text Colour"));
1455 editMenu
->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, _("Change Background Colour"));
1456 editMenu
->AppendSeparator();
1457 editMenu
->Append(RICHTEXT_LEFT_INDENT
, _("Left Indent"));
1458 editMenu
->Append(RICHTEXT_RIGHT_INDENT
, _("Right indent"));
1459 menuBar
->Append(editMenu
, _("Edit"));
1461 SetMenuBar(menuBar
);
1464 #endif // wxUSE_STATUSBAR
1469 void RichTextFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
1474 void RichTextFrame::OnLeftAlign(wxCommandEvent
& WXUNUSED(event
))
1477 attr
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
1480 m_textCtrl
->GetSelection(& start
, & end
);
1481 m_textCtrl
->SetStyle(start
, end
, attr
);
1483 m_currentPosition
= -1;
1486 void RichTextFrame::OnRightAlign(wxCommandEvent
& WXUNUSED(event
))
1489 attr
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1492 m_textCtrl
->GetSelection(& start
, & end
);
1493 m_textCtrl
->SetStyle(start
, end
, attr
);
1495 m_currentPosition
= -1;
1498 void RichTextFrame::OnJustify(wxCommandEvent
& WXUNUSED(event
))
1501 attr
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
1504 m_textCtrl
->GetSelection(& start
, & end
);
1505 m_textCtrl
->SetStyle(start
, end
, attr
);
1507 m_currentPosition
= -1;
1510 void RichTextFrame::OnCentre(wxCommandEvent
& WXUNUSED(event
))
1513 attr
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1516 m_textCtrl
->GetSelection(& start
, & end
);
1517 m_textCtrl
->SetStyle(start
, end
, attr
);
1519 m_currentPosition
= -1;
1522 void RichTextFrame::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
1526 wxFontDialog
dialog(this, data
);
1528 if (dialog
.ShowModal() == wxID_OK
)
1530 wxFontData retData
= dialog
.GetFontData();
1531 wxFont font
= retData
.GetChosenFont();
1537 m_textCtrl
->GetSelection(& start
, & end
);
1538 m_textCtrl
->SetStyle(start
, end
, attr
);
1540 m_currentPosition
= -1;
1544 void RichTextFrame::OnChangeTextColour(wxCommandEvent
& WXUNUSED(event
))
1547 data
.SetColour(* wxBLACK
);
1548 data
.SetChooseFull(true);
1549 for (int i
= 0; i
< 16; i
++)
1551 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1552 data
.SetCustomColour(i
, colour
);
1555 wxColourDialog
dialog(this, &data
);
1556 dialog
.SetTitle(_T("Choose the text colour"));
1557 if (dialog
.ShowModal() == wxID_OK
)
1559 wxColourData retData
= dialog
.GetColourData();
1560 wxColour col
= retData
.GetColour();
1563 attr
.SetTextColour(col
);
1566 m_textCtrl
->GetSelection(& start
, & end
);
1567 m_textCtrl
->SetStyle(start
, end
, attr
);
1569 m_currentPosition
= -1;
1573 void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
1576 data
.SetColour(* wxWHITE
);
1577 data
.SetChooseFull(true);
1578 for (int i
= 0; i
< 16; i
++)
1580 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1581 data
.SetCustomColour(i
, colour
);
1584 wxColourDialog
dialog(this, &data
);
1585 dialog
.SetTitle(_T("Choose the text background colour"));
1586 if (dialog
.ShowModal() == wxID_OK
)
1588 wxColourData retData
= dialog
.GetColourData();
1589 wxColour col
= retData
.GetColour();
1592 attr
.SetBackgroundColour(col
);
1595 m_textCtrl
->GetSelection(& start
, & end
);
1596 m_textCtrl
->SetStyle(start
, end
, attr
);
1598 m_currentPosition
= -1;
1602 void RichTextFrame::OnLeftIndent(wxCommandEvent
& WXUNUSED(event
))
1604 wxString indentStr
= wxGetTextFromUser
1606 _("Please enter the left indent in tenths of a millimetre."),
1611 if (!indentStr
.IsEmpty())
1613 int indent
= wxAtoi(indentStr
);
1616 attr
.SetLeftIndent(indent
);
1619 m_textCtrl
->GetSelection(& start
, & end
);
1620 m_textCtrl
->SetStyle(start
, end
, attr
);
1622 m_currentPosition
= -1;
1626 void RichTextFrame::OnRightIndent(wxCommandEvent
& WXUNUSED(event
))
1628 wxString indentStr
= wxGetTextFromUser
1630 _("Please enter the right indent in tenths of a millimetre."),
1635 if (!indentStr
.IsEmpty())
1637 int indent
= wxAtoi(indentStr
);
1640 attr
.SetRightIndent(indent
);
1643 m_textCtrl
->GetSelection(& start
, & end
);
1644 m_textCtrl
->SetStyle(start
, end
, attr
);
1646 m_currentPosition
= -1;
1650 void RichTextFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1652 long insertionPoint
= m_textCtrl
->GetInsertionPoint();
1653 if (insertionPoint
!= m_currentPosition
)
1657 if (m_textCtrl
->GetStyle(insertionPoint
, attr
))
1660 wxString
facename(wxT("unknown"));
1661 if (attr
.GetFont().Ok())
1663 facename
= attr
.GetFont().GetFaceName();
1665 wxString
alignment(wxT("unknown alignment"));
1666 if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1667 alignment
= wxT("centred");
1668 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
1669 alignment
= wxT("right-aligned");
1670 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1671 alignment
= wxT("left-aligned");
1672 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
1673 alignment
= wxT("justified");
1674 msg
.Printf(wxT("Facename: %s, wxColour(%d, %d, %d), %s"),
1675 (const wxChar
*) facename
,
1676 attr
.GetTextColour().Red(), attr
.GetTextColour().Green(), attr
.GetTextColour().Blue(),
1677 (const wxChar
*) alignment
);
1681 if (attr
.GetFont().GetWeight() == wxBOLD
)
1682 msg
+= wxT(" BOLD");
1683 else if (attr
.GetFont().GetWeight() == wxNORMAL
)
1684 msg
+= wxT(" NORMAL");
1686 if (attr
.GetFont().GetStyle() == wxITALIC
)
1687 msg
+= wxT(" ITALIC");
1689 if (attr
.GetFont().GetUnderlined())
1690 msg
+= wxT(" UNDERLINED");
1695 #endif // wxUSE_STATUSBAR
1696 m_currentPosition
= insertionPoint
;