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();
119 // return true if currently text control has any selection
120 bool HasSelection() const
123 GetFocusedText()->GetSelection(&from
, &to
);
128 MyTextCtrl
*m_password
;
131 MyTextCtrl
*m_readonly
;
132 MyTextCtrl
*m_limited
;
134 MyTextCtrl
*m_multitext
;
135 MyTextCtrl
*m_horizontal
;
137 MyTextCtrl
*m_textrich
;
145 // get the currently focused text control or return the default one
146 // (m_multitext) is no text ctrl has focus -- in any case, returns
147 // something non NULL
148 wxTextCtrl
*GetFocusedText() const;
151 class MyFrame
: public wxFrame
154 MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
156 void OnQuit(wxCommandEvent
& event
);
157 void OnAbout(wxCommandEvent
& event
);
159 void OnSetTooltipDelay(wxCommandEvent
& event
);
160 void OnToggleTooltips(wxCommandEvent
& event
);
161 #endif // wxUSE_TOOLTIPS
164 void OnPasteFromClipboard( wxCommandEvent
& WXUNUSED(event
) )
166 wxLogMessage(_T("Pasting text from clipboard."));
167 m_panel
->DoPasteFromClipboard();
169 void OnCopyToClipboard( wxCommandEvent
& WXUNUSED(event
) )
171 wxLogMessage(_T("Copying text to clipboard."));
172 m_panel
->DoCopyToClipboard();
175 void OnUpdatePasteFromClipboard(wxUpdateUIEvent
& event
)
177 wxClipboardLocker lockClip
;
179 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
182 void OnUpdateCopyToClipboard(wxUpdateUIEvent
& event
)
184 event
.Enable( m_panel
->HasSelection() );
186 #endif // wxUSE_CLIPBOARD
188 void OnAddTextFreeze( wxCommandEvent
& WXUNUSED(event
) )
190 void OnAddText( wxCommandEvent
& WXUNUSED(event
) )
191 { DoAddText(false); }
192 void OnRemoveText( wxCommandEvent
& WXUNUSED(event
) )
193 { m_panel
->DoRemoveText(); }
194 void OnReplaceText( wxCommandEvent
& WXUNUSED(event
) )
195 { m_panel
->DoReplaceText(); }
196 void OnSelectText( wxCommandEvent
& WXUNUSED(event
) )
197 { m_panel
->DoSelectText(); }
199 void OnMoveToEndOfText( wxCommandEvent
& WXUNUSED(event
) )
200 { m_panel
->DoMoveToEndOfText(); }
201 void OnMoveToEndOfEntry( wxCommandEvent
& WXUNUSED(event
) )
202 { m_panel
->DoMoveToEndOfEntry(); }
204 void OnScrollLineDown(wxCommandEvent
& WXUNUSED(event
))
206 if ( !m_panel
->m_textrich
->LineDown() )
207 wxLogMessage(_T("Already at the bottom"));
210 void OnScrollLineUp(wxCommandEvent
& WXUNUSED(event
))
212 if ( !m_panel
->m_textrich
->LineUp() )
213 wxLogMessage(_T("Already at the top"));
216 void OnScrollPageDown(wxCommandEvent
& WXUNUSED(event
))
218 if ( !m_panel
->m_textrich
->PageDown() )
219 wxLogMessage(_T("Already at the bottom"));
222 void OnScrollPageUp(wxCommandEvent
& WXUNUSED(event
))
224 if ( !m_panel
->m_textrich
->PageUp() )
225 wxLogMessage(_T("Already at the top"));
228 void OnGetLine(wxCommandEvent
& WXUNUSED(event
))
230 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
231 wxT("Enter which line you would like to get"),
232 wxT("Get a line from the tabbed multiline text control") );
234 wxMessageBox(m_panel
->m_tab
->GetLineText(nLine
));
237 void OnGetLineLength(wxCommandEvent
& WXUNUSED(event
))
239 long nLine
= wxGetNumberFromUser(wxT("Which line would you like to get?"),
240 wxT("Enter which line you would like to get"),
241 wxT("Get length of a line from the tabbed multiline text control") );
243 wxMessageBox(wxString::Format(wxT("Length of line %i is:%i"),
245 m_panel
->m_tab
->GetLineLength(nLine
))
250 void OnLogClear(wxCommandEvent
& event
);
252 void OnFileSave(wxCommandEvent
& event
);
253 void OnFileLoad(wxCommandEvent
& event
);
254 void OnRichTextTest(wxCommandEvent
& event
);
256 void OnSetEditable(wxCommandEvent
& event
);
257 void OnSetEnabled(wxCommandEvent
& event
);
259 void OnLogKey(wxCommandEvent
& event
)
261 MyTextCtrl::ms_logKey
= event
.IsChecked();
264 void OnLogChar(wxCommandEvent
& event
)
266 MyTextCtrl::ms_logChar
= event
.IsChecked();
269 void OnLogMouse(wxCommandEvent
& event
)
271 MyTextCtrl::ms_logMouse
= event
.IsChecked();
274 void OnLogText(wxCommandEvent
& event
)
276 MyTextCtrl::ms_logText
= event
.IsChecked();
279 void OnLogFocus(wxCommandEvent
& event
)
281 MyTextCtrl::ms_logFocus
= event
.IsChecked();
284 void OnSetText(wxCommandEvent
& WXUNUSED(event
))
286 m_panel
->m_text
->SetValue(_T("Hello, world (what else did you expect)?"));
289 void OnIdle( wxIdleEvent
& event
);
292 void DoAddText(bool freeze
)
294 wxTextCtrl
* const text
= m_panel
->m_textrich
;
300 for ( int i
= 0; i
< 100; i
++ )
302 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
308 text
->SetInsertionPoint(0);
313 DECLARE_EVENT_TABLE()
317 * RichTextFrame is used to demonstrate rich text behaviour
320 class RichTextFrame
: public wxFrame
323 RichTextFrame(wxWindow
* parent
, const wxString
& title
);
327 void OnClose(wxCommandEvent
& event
);
328 void OnIdle(wxIdleEvent
& event
);
329 void OnLeftAlign(wxCommandEvent
& event
);
330 void OnRightAlign(wxCommandEvent
& event
);
331 void OnJustify(wxCommandEvent
& event
);
332 void OnCentre(wxCommandEvent
& event
);
333 void OnChangeFont(wxCommandEvent
& event
);
334 void OnChangeTextColour(wxCommandEvent
& event
);
335 void OnChangeBackgroundColour(wxCommandEvent
& event
);
336 void OnLeftIndent(wxCommandEvent
& event
);
337 void OnRightIndent(wxCommandEvent
& event
);
340 wxTextCtrl
*m_textCtrl
;
341 long m_currentPosition
;
343 DECLARE_EVENT_TABLE()
346 //----------------------------------------------------------------------
348 //----------------------------------------------------------------------
352 //----------------------------------------------------------------------
354 //----------------------------------------------------------------------
358 TEXT_QUIT
= wxID_EXIT
,
359 TEXT_ABOUT
= wxID_ABOUT
,
366 TEXT_CLIPBOARD_COPY
= 200,
367 TEXT_CLIPBOARD_PASTE
,
370 TEXT_TOOLTIPS_SETDELAY
= 300,
371 TEXT_TOOLTIPS_ENABLE
,
405 // Create the main frame window
406 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
407 _T("Text wxWidgets sample"), 50, 50, 700, 550);
408 frame
->SetSizeHints( 500, 400 );
410 wxMenu
*file_menu
= new wxMenu
;
411 file_menu
->Append(TEXT_SAVE
, _T("&Save file\tCtrl-S"),
412 _T("Save the text control contents to file"));
413 file_menu
->Append(TEXT_LOAD
, _T("&Load file\tCtrl-O"),
414 _T("Load the sample file into text control"));
415 file_menu
->AppendSeparator();
416 file_menu
->Append(TEXT_RICH_TEXT_TEST
, _T("Show Rich Text Editor"));
417 file_menu
->AppendSeparator();
418 file_menu
->Append(TEXT_ABOUT
, _T("&About\tAlt-A"));
419 file_menu
->AppendSeparator();
420 file_menu
->Append(TEXT_QUIT
, _T("E&xit\tAlt-X"), _T("Quit this sample"));
422 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
423 menu_bar
->Append(file_menu
, _T("&File"));
426 wxMenu
*tooltip_menu
= new wxMenu
;
427 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, _T("Set &delay\tCtrl-D"));
428 tooltip_menu
->AppendSeparator();
429 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, _T("&Toggle tooltips\tCtrl-T"),
430 _T("enable/disable tooltips"), true);
431 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, true);
432 menu_bar
->Append(tooltip_menu
, _T("&Tooltips"));
433 #endif // wxUSE_TOOLTIPS
436 // notice that we use non default accelerators on purpose here to compare
437 // their behaviour with the built in handling of standard Ctrl/Cmd-C/V
438 wxMenu
*menuClipboard
= new wxMenu
;
439 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, _T("&Copy\tCtrl-Shift-C"),
440 _T("Copy the selection to the clipboard"));
441 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, _T("&Paste\tCtrl-Shift-V"),
442 _T("Paste from clipboard to the text control"));
443 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
444 #endif // wxUSE_CLIPBOARD
446 wxMenu
*menuText
= new wxMenu
;
447 menuText
->Append(TEXT_ADD_SOME
, _T("&Append some text\tCtrl-A"));
448 menuText
->Append(TEXT_ADD_FREEZE
, _T("&Append text with freeze/thaw\tShift-Ctrl-A"));
449 menuText
->Append(TEXT_REMOVE
, _T("&Remove first 10 characters\tCtrl-Y"));
450 menuText
->Append(TEXT_REPLACE
, _T("&Replace characters 4 to 8 with ABC\tCtrl-R"));
451 menuText
->Append(TEXT_SELECT
, _T("&Select characters 4 to 8\tCtrl-I"));
452 menuText
->Append(TEXT_SET
, _T("&Set the first text zone value\tCtrl-E"));
453 menuText
->AppendSeparator();
454 menuText
->Append(TEXT_MOVE_ENDTEXT
, _T("Move cursor to the end of &text"));
455 menuText
->Append(TEXT_MOVE_ENDENTRY
, _T("Move cursor to the end of &entry"));
456 menuText
->AppendCheckItem(TEXT_SET_EDITABLE
, _T("Toggle &editable state"));
457 menuText
->AppendCheckItem(TEXT_SET_ENABLED
, _T("Toggle e&nabled state"));
458 menuText
->Check(TEXT_SET_EDITABLE
, true);
459 menuText
->Check(TEXT_SET_ENABLED
, true);
460 menuText
->AppendSeparator();
461 menuText
->Append(TEXT_LINE_DOWN
, _T("Scroll text one line down"));
462 menuText
->Append(TEXT_LINE_UP
, _T("Scroll text one line up"));
463 menuText
->Append(TEXT_PAGE_DOWN
, _T("Scroll text one page down"));
464 menuText
->Append(TEXT_PAGE_UP
, _T("Scroll text one page up"));
465 menuText
->AppendSeparator();
466 menuText
->Append(TEXT_GET_LINE
, _T("Get the text of a line of the tabbed multiline"));
467 menuText
->Append(TEXT_GET_LINELENGTH
, _T("Get the length of a line of the tabbed multiline"));
468 menu_bar
->Append(menuText
, _T("Te&xt"));
471 wxMenu
*menuLog
= new wxMenu
;
472 menuLog
->AppendCheckItem(TEXT_LOG_KEY
, _T("Log &key events"));
473 menuLog
->AppendCheckItem(TEXT_LOG_CHAR
, _T("Log &char events"));
474 menuLog
->AppendCheckItem(TEXT_LOG_MOUSE
, _T("Log &mouse events"));
475 menuLog
->AppendCheckItem(TEXT_LOG_TEXT
, _T("Log &text events"));
476 menuLog
->AppendCheckItem(TEXT_LOG_FOCUS
, _T("Log &focus events"));
477 menuLog
->AppendSeparator();
478 menuLog
->Append(TEXT_CLEAR
, _T("&Clear the log\tCtrl-L"),
479 _T("Clear the log window contents"));
481 // select only the interesting events by default
482 MyTextCtrl::ms_logKey
=
483 MyTextCtrl::ms_logChar
= false;
484 MyTextCtrl::ms_logText
= true;
486 menuLog
->Check(TEXT_LOG_KEY
, MyTextCtrl::ms_logKey
);
487 menuLog
->Check(TEXT_LOG_CHAR
, MyTextCtrl::ms_logChar
);
488 menuLog
->Check(TEXT_LOG_TEXT
, MyTextCtrl::ms_logText
);
490 menu_bar
->Append(menuLog
, _T("&Log"));
493 frame
->SetMenuBar(menu_bar
);
503 //----------------------------------------------------------------------
505 //----------------------------------------------------------------------
507 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
508 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
509 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
510 EVT_CHAR(MyTextCtrl::OnChar
)
512 EVT_TEXT(wxID_ANY
, MyTextCtrl::OnText
)
513 EVT_TEXT_URL(wxID_ANY
, MyTextCtrl::OnTextURL
)
514 EVT_TEXT_MAXLEN(wxID_ANY
, MyTextCtrl::OnTextMaxLen
)
516 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
518 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus
)
519 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus
)
522 bool MyTextCtrl::ms_logKey
= false;
523 bool MyTextCtrl::ms_logChar
= false;
524 bool MyTextCtrl::ms_logMouse
= false;
525 bool MyTextCtrl::ms_logText
= false;
526 bool MyTextCtrl::ms_logFocus
= false;
528 void MyTextCtrl::LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const
531 long keycode
= event
.GetKeyCode();
535 case WXK_BACK
: key
= _T("BACK"); break;
536 case WXK_TAB
: key
= _T("TAB"); break;
537 case WXK_RETURN
: key
= _T("RETURN"); break;
538 case WXK_ESCAPE
: key
= _T("ESCAPE"); break;
539 case WXK_SPACE
: key
= _T("SPACE"); break;
540 case WXK_DELETE
: key
= _T("DELETE"); break;
541 case WXK_START
: key
= _T("START"); break;
542 case WXK_LBUTTON
: key
= _T("LBUTTON"); break;
543 case WXK_RBUTTON
: key
= _T("RBUTTON"); break;
544 case WXK_CANCEL
: key
= _T("CANCEL"); break;
545 case WXK_MBUTTON
: key
= _T("MBUTTON"); break;
546 case WXK_CLEAR
: key
= _T("CLEAR"); break;
547 case WXK_SHIFT
: key
= _T("SHIFT"); break;
548 case WXK_ALT
: key
= _T("ALT"); break;
549 case WXK_CONTROL
: key
= _T("CONTROL"); break;
550 case WXK_MENU
: key
= _T("MENU"); break;
551 case WXK_PAUSE
: key
= _T("PAUSE"); break;
552 case WXK_CAPITAL
: key
= _T("CAPITAL"); break;
553 case WXK_PRIOR
: key
= _T("PRIOR"); break;
554 case WXK_NEXT
: key
= _T("NEXT"); break;
555 case WXK_END
: key
= _T("END"); break;
556 case WXK_HOME
: key
= _T("HOME"); break;
557 case WXK_LEFT
: key
= _T("LEFT"); break;
558 case WXK_UP
: key
= _T("UP"); break;
559 case WXK_RIGHT
: key
= _T("RIGHT"); break;
560 case WXK_DOWN
: key
= _T("DOWN"); break;
561 case WXK_SELECT
: key
= _T("SELECT"); break;
562 case WXK_PRINT
: key
= _T("PRINT"); break;
563 case WXK_EXECUTE
: key
= _T("EXECUTE"); break;
564 case WXK_SNAPSHOT
: key
= _T("SNAPSHOT"); break;
565 case WXK_INSERT
: key
= _T("INSERT"); break;
566 case WXK_HELP
: key
= _T("HELP"); break;
567 case WXK_NUMPAD0
: key
= _T("NUMPAD0"); break;
568 case WXK_NUMPAD1
: key
= _T("NUMPAD1"); break;
569 case WXK_NUMPAD2
: key
= _T("NUMPAD2"); break;
570 case WXK_NUMPAD3
: key
= _T("NUMPAD3"); break;
571 case WXK_NUMPAD4
: key
= _T("NUMPAD4"); break;
572 case WXK_NUMPAD5
: key
= _T("NUMPAD5"); break;
573 case WXK_NUMPAD6
: key
= _T("NUMPAD6"); break;
574 case WXK_NUMPAD7
: key
= _T("NUMPAD7"); break;
575 case WXK_NUMPAD8
: key
= _T("NUMPAD8"); break;
576 case WXK_NUMPAD9
: key
= _T("NUMPAD9"); break;
577 case WXK_MULTIPLY
: key
= _T("MULTIPLY"); break;
578 case WXK_ADD
: key
= _T("ADD"); break;
579 case WXK_SEPARATOR
: key
= _T("SEPARATOR"); break;
580 case WXK_SUBTRACT
: key
= _T("SUBTRACT"); break;
581 case WXK_DECIMAL
: key
= _T("DECIMAL"); break;
582 case WXK_DIVIDE
: key
= _T("DIVIDE"); break;
583 case WXK_F1
: key
= _T("F1"); break;
584 case WXK_F2
: key
= _T("F2"); break;
585 case WXK_F3
: key
= _T("F3"); break;
586 case WXK_F4
: key
= _T("F4"); break;
587 case WXK_F5
: key
= _T("F5"); break;
588 case WXK_F6
: key
= _T("F6"); break;
589 case WXK_F7
: key
= _T("F7"); break;
590 case WXK_F8
: key
= _T("F8"); break;
591 case WXK_F9
: key
= _T("F9"); break;
592 case WXK_F10
: key
= _T("F10"); break;
593 case WXK_F11
: key
= _T("F11"); break;
594 case WXK_F12
: key
= _T("F12"); break;
595 case WXK_F13
: key
= _T("F13"); break;
596 case WXK_F14
: key
= _T("F14"); break;
597 case WXK_F15
: key
= _T("F15"); break;
598 case WXK_F16
: key
= _T("F16"); break;
599 case WXK_F17
: key
= _T("F17"); break;
600 case WXK_F18
: key
= _T("F18"); break;
601 case WXK_F19
: key
= _T("F19"); break;
602 case WXK_F20
: key
= _T("F20"); break;
603 case WXK_F21
: key
= _T("F21"); break;
604 case WXK_F22
: key
= _T("F22"); break;
605 case WXK_F23
: key
= _T("F23"); break;
606 case WXK_F24
: key
= _T("F24"); break;
607 case WXK_NUMLOCK
: key
= _T("NUMLOCK"); break;
608 case WXK_SCROLL
: key
= _T("SCROLL"); break;
609 case WXK_PAGEUP
: key
= _T("PAGEUP"); break;
610 case WXK_PAGEDOWN
: key
= _T("PAGEDOWN"); break;
611 case WXK_NUMPAD_SPACE
: key
= _T("NUMPAD_SPACE"); break;
612 case WXK_NUMPAD_TAB
: key
= _T("NUMPAD_TAB"); break;
613 case WXK_NUMPAD_ENTER
: key
= _T("NUMPAD_ENTER"); break;
614 case WXK_NUMPAD_F1
: key
= _T("NUMPAD_F1"); break;
615 case WXK_NUMPAD_F2
: key
= _T("NUMPAD_F2"); break;
616 case WXK_NUMPAD_F3
: key
= _T("NUMPAD_F3"); break;
617 case WXK_NUMPAD_F4
: key
= _T("NUMPAD_F4"); break;
618 case WXK_NUMPAD_HOME
: key
= _T("NUMPAD_HOME"); break;
619 case WXK_NUMPAD_LEFT
: key
= _T("NUMPAD_LEFT"); break;
620 case WXK_NUMPAD_UP
: key
= _T("NUMPAD_UP"); break;
621 case WXK_NUMPAD_RIGHT
: key
= _T("NUMPAD_RIGHT"); break;
622 case WXK_NUMPAD_DOWN
: key
= _T("NUMPAD_DOWN"); break;
623 case WXK_NUMPAD_PRIOR
: key
= _T("NUMPAD_PRIOR"); break;
624 case WXK_NUMPAD_PAGEUP
: key
= _T("NUMPAD_PAGEUP"); break;
625 case WXK_NUMPAD_PAGEDOWN
: key
= _T("NUMPAD_PAGEDOWN"); break;
626 case WXK_NUMPAD_END
: key
= _T("NUMPAD_END"); break;
627 case WXK_NUMPAD_BEGIN
: key
= _T("NUMPAD_BEGIN"); break;
628 case WXK_NUMPAD_INSERT
: key
= _T("NUMPAD_INSERT"); break;
629 case WXK_NUMPAD_DELETE
: key
= _T("NUMPAD_DELETE"); break;
630 case WXK_NUMPAD_EQUAL
: key
= _T("NUMPAD_EQUAL"); break;
631 case WXK_NUMPAD_MULTIPLY
: key
= _T("NUMPAD_MULTIPLY"); break;
632 case WXK_NUMPAD_ADD
: key
= _T("NUMPAD_ADD"); break;
633 case WXK_NUMPAD_SEPARATOR
: key
= _T("NUMPAD_SEPARATOR"); break;
634 case WXK_NUMPAD_SUBTRACT
: key
= _T("NUMPAD_SUBTRACT"); break;
635 case WXK_NUMPAD_DECIMAL
: key
= _T("NUMPAD_DECIMAL"); break;
639 if ( wxIsprint((int)keycode
) )
640 key
.Printf(_T("'%c'"), (char)keycode
);
641 else if ( keycode
> 0 && keycode
< 27 )
642 key
.Printf(_("Ctrl-%c"), _T('A') + keycode
- 1);
644 key
.Printf(_T("unknown (%ld)"), keycode
);
650 key
+= wxString::Format(_T(" (Unicode: %#04x)"), event
.GetUnicodeKey());
651 #endif // wxUSE_UNICODE
653 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
656 GetChar( event
.ControlDown(), _T('C') ),
657 GetChar( event
.AltDown(), _T('A') ),
658 GetChar( event
.ShiftDown(), _T('S') ),
659 GetChar( event
.MetaDown(), _T('M') ) );
662 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
667 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
670 dbl
= ev
.LeftDClick();
673 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
675 button
= _T("Middle");
676 dbl
= ev
.MiddleDClick();
679 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
681 button
= _T("Right");
682 dbl
= ev
.RightDClick();
687 return _T("Unknown mouse event");
690 return wxString::Format(_T("%s mouse button %s"),
692 dbl
? _T("double clicked")
693 : up
? _T("released") : _T("clicked"));
696 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
708 msg
= _T("Mouse entered the window");
710 else if ( ev
.Leaving() )
712 msg
= _T("Mouse left the window");
716 msg
= GetMouseEventDesc(ev
);
719 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ");
722 wxTextCtrlHitTestResult rc
= HitTest(ev
.GetPosition(), &pos
);
723 if ( rc
!= wxTE_HT_UNKNOWN
)
725 msg
<< _T("at position ") << pos
<< _T(' ');
728 msg
<< _T("[Flags: ")
729 << GetChar( ev
.LeftIsDown(), _T('1') )
730 << GetChar( ev
.MiddleIsDown(), _T('2') )
731 << GetChar( ev
.RightIsDown(), _T('3') )
732 << GetChar( ev
.ControlDown(), _T('C') )
733 << GetChar( ev
.AltDown(), _T('A') )
734 << GetChar( ev
.ShiftDown(), _T('S') )
735 << GetChar( ev
.MetaDown(), _T('M') )
740 //else: we're not interested in mouse move events
743 void MyTextCtrl::OnSetFocus(wxFocusEvent
& event
)
746 wxLogMessage( wxT("%p got focus."), this);
751 void MyTextCtrl::OnKillFocus(wxFocusEvent
& event
)
754 wxLogMessage( wxT("%p lost focus"), this);
759 void MyTextCtrl::OnText(wxCommandEvent
& event
)
764 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
765 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
768 wxLogMessage(_T("Text changed in control '%s'"), data
);
772 // wxLogMessage( event.GetString() );
773 wxLogMessage(_T("Text changed in some control"));
777 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& WXUNUSED(event
))
779 wxLogMessage(_T("You can't enter more characters into this control."));
782 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
784 const wxMouseEvent
& ev
= event
.GetMouseEvent();
786 // filter out mouse moves, too many of them
790 long start
= event
.GetURLStart(),
791 end
= event
.GetURLEnd();
793 wxLogMessage(_T("Mouse event over URL '%s': %s"),
794 GetValue().Mid(start
, end
- start
).c_str(),
795 GetMouseEventDesc(ev
).c_str());
798 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
801 LogKeyEvent( _T("Char"), event
);
806 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
809 LogKeyEvent( _T("Key up"), event
);
814 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
816 switch ( event
.GetKeyCode() )
819 // show current position and text length
821 long line
, column
, pos
= GetInsertionPoint();
822 PositionToXY(pos
, &column
, &line
);
824 wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
827 (long) GetNumberOfLines(),
828 (long) GetLineLength(line
),
829 (unsigned int) GetValue().length(),
833 GetSelection(&from
, &to
);
835 wxString sel
= GetStringSelection();
837 wxLogMessage(_T("Selection: from %ld to %ld."), from
, to
);
838 wxLogMessage(_T("Selection = '%s' (len = %u)"),
840 (unsigned int) sel
.length());
846 SetInsertionPointEnd();
851 SetInsertionPoint(10);
857 wxLogDebug( wxT("Now capturing mouse and events.") );
863 wxLogDebug( wxT("Stopped capturing mouse and events.") );
864 m_hasCapture
= false;
870 // insert a blank line
875 wxLogMessage(_T("IsModified() before SetValue(): %d"),
877 SetValue(_T("SetValue() has been called"));
878 wxLogMessage(_T("IsModified() after SetValue(): %d"),
883 wxLogMessage(_T("Position 10 should be now visible."));
888 wxLogMessage(_T("Control has been cleared"));
893 WriteText(_T("WriteText() has been called"));
897 AppendText(_T("AppendText() has been called"));
902 LogKeyEvent( wxT("Key down"), event
);
907 //----------------------------------------------------------------------
909 //----------------------------------------------------------------------
911 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
912 : wxPanel( frame
, wxID_ANY
, wxPoint(x
, y
), wxSize(w
, h
) )
915 m_log
= new wxTextCtrl( this, wxID_ANY
, _T("This is the log window.\n"),
916 wxPoint(5,260), wxSize(630,100),
917 wxTE_MULTILINE
| wxTE_READONLY
/* | wxTE_RICH */);
919 m_logOld
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
922 // single line text controls
924 m_text
= new MyTextCtrl( this, wxID_ANY
, _T("Single line."),
925 wxDefaultPosition
, wxDefaultSize
,
927 m_text
->SetForegroundColour(*wxBLUE
);
928 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
929 (*m_text
) << _T(" Appended.");
930 m_text
->SetInsertionPoint(0);
931 m_text
->WriteText( _T("Prepended. ") );
933 m_password
= new MyTextCtrl( this, wxID_ANY
, _T(""),
934 wxPoint(10,50), wxSize(140,wxDefaultCoord
), wxTE_PASSWORD
);
936 m_readonly
= new MyTextCtrl( this, wxID_ANY
, _T("Read only"),
937 wxPoint(10,90), wxSize(140,wxDefaultCoord
), wxTE_READONLY
);
939 m_limited
= new MyTextCtrl(this, wxID_ANY
, _T("Max 8 ch"),
940 wxPoint(10, 130), wxSize(140, wxDefaultCoord
));
941 m_limited
->SetMaxLength(8);
943 // multi line text controls
945 m_horizontal
= new MyTextCtrl( this, wxID_ANY
, _T("Multiline text control with a horizontal scrollbar.\n"),
946 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
948 // a little hack to use the command line argument for encoding testing
949 if ( wxTheApp
->argc
== 2 )
951 switch ( wxTheApp
->argv
[1][0] )
954 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
956 wxFONTENCODING_ISO8859_2
));
957 m_horizontal
->AppendText(_T("\256lu\273ou\350k\375 k\371\362 zb\354sile \350e\271tina \253\273"));
961 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
963 wxFONTENCODING_CP1251
));
964 m_horizontal
->AppendText(_T("\317\360\350\342\345\362!"));
968 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
970 wxFONTENCODING_CP1251
));
972 m_horizontal
->AppendText(L
"\x0412\x0430\x0434\x0438\x043c \x0426");
974 m_horizontal
->AppendText("\313\301\326\305\324\323\321 \325\304\301\336\316\331\315");
980 m_horizontal
->AppendText(_T("Text in default encoding"));
983 m_multitext
= new MyTextCtrl( this, wxID_ANY
, _T("Multi line."),
984 wxPoint(180,10), wxSize(200,70), wxTE_MULTILINE
);
985 m_multitext
->SetFont(*wxITALIC_FONT
);
986 (*m_multitext
) << _T(" Appended.");
987 m_multitext
->SetInsertionPoint(0);
988 m_multitext
->WriteText( _T("Prepended. ") );
989 m_multitext
->SetForegroundColour(*wxRED
);
990 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
993 m_multitext
->SetToolTip(_T("Press Fn function keys here"));
996 m_tab
= new MyTextCtrl( this, 100, _T("Multiline, allow <TAB> processing."),
997 wxPoint(180,90), wxSize(200,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
998 m_tab
->SetClientData((void *)_T("tab"));
1000 m_enter
= new MyTextCtrl( this, 100, _T("Multiline, allow <ENTER> processing."),
1001 wxPoint(180,170), wxSize(200,70), wxTE_MULTILINE
);
1002 m_enter
->SetClientData((void *)_T("enter"));
1004 m_textrich
= new MyTextCtrl(this, wxID_ANY
, _T("Allows more than 30Kb of text\n")
1005 _T("(even under broken Win9x)\n")
1006 _T("and a very very very very very ")
1007 _T("very very very long line to test ")
1008 _T("wxHSCROLL style\n")
1009 _T("\nAnd here is a link in quotation marks to ")
1010 _T("test wxTE_AUTO_URL: \"http://www.wxwidgets.org\""),
1011 wxPoint(450, 10), wxSize(200, 230),
1012 wxTE_RICH
| wxTE_MULTILINE
| wxTE_AUTO_URL
);
1013 m_textrich
->SetStyle(0, 10, *wxRED
);
1014 m_textrich
->SetStyle(10, 20, *wxBLUE
);
1015 m_textrich
->SetStyle(30, 40,
1016 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
1017 m_textrich
->SetDefaultStyle(wxTextAttr());
1018 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
1019 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
1020 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
1021 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
1022 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
1023 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
1024 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
, *wxWHITE
));
1025 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
1026 _T("type should be in blue as well"));
1029 // lay out the controls
1030 wxBoxSizer
*column1
= new wxBoxSizer(wxVERTICAL
);
1031 column1
->Add( m_text
, 0, wxALL
| wxEXPAND
, 10 );
1032 column1
->Add( m_password
, 0, wxALL
| wxEXPAND
, 10 );
1033 column1
->Add( m_readonly
, 0, wxALL
| wxEXPAND
, 10 );
1034 column1
->Add( m_limited
, 0, wxALL
| wxEXPAND
, 10 );
1035 column1
->Add( m_horizontal
, 1, wxALL
| wxEXPAND
, 10 );
1037 wxBoxSizer
*column2
= new wxBoxSizer(wxVERTICAL
);
1038 column2
->Add( m_multitext
, 1, wxALL
| wxEXPAND
, 10 );
1039 column2
->Add( m_tab
, 1, wxALL
| wxEXPAND
, 10 );
1040 column2
->Add( m_enter
, 1, wxALL
| wxEXPAND
, 10 );
1042 wxBoxSizer
*row1
= new wxBoxSizer(wxHORIZONTAL
);
1043 row1
->Add( column1
, 0, wxALL
| wxEXPAND
, 10 );
1044 row1
->Add( column2
, 1, wxALL
| wxEXPAND
, 10 );
1045 row1
->Add( m_textrich
, 1, wxALL
| wxEXPAND
, 10 );
1047 wxBoxSizer
*topSizer
= new wxBoxSizer(wxVERTICAL
);
1048 topSizer
->Add( row1
, 2, wxALL
| wxEXPAND
, 10 );
1051 topSizer
->Add( m_log
, 1, wxALL
| wxEXPAND
, 10 );
1054 SetAutoLayout( true );
1058 wxTextCtrl
*MyPanel::GetFocusedText() const
1060 wxWindow
*win
= FindFocus();
1062 wxTextCtrl
*text
= win
? wxDynamicCast(win
, wxTextCtrl
) : NULL
;
1063 return text
? text
: m_multitext
;
1067 void MyPanel::DoPasteFromClipboard()
1069 // On X11, we want to get the data from the primary selection instead
1070 // of the normal clipboard (which isn't normal under X11 at all). This
1071 // call has no effect under MSW.
1072 wxTheClipboard
->UsePrimarySelection();
1074 if (!wxTheClipboard
->Open())
1077 *m_log
<< _T("Error opening the clipboard.\n");
1084 *m_log
<< _T("Successfully opened the clipboard.\n");
1088 wxTextDataObject data
;
1090 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
1093 *m_log
<< _T("Clipboard supports requested format.\n");
1096 if (wxTheClipboard
->GetData( data
))
1099 *m_log
<< _T("Successfully retrieved data from the clipboard.\n");
1101 GetFocusedText()->AppendText(data
.GetText());
1106 *m_log
<< _T("Error getting data from the clipboard.\n");
1113 *m_log
<< _T("Clipboard doesn't support requested format.\n");
1117 wxTheClipboard
->Close();
1120 *m_log
<< _T("Closed the clipboard.\n");
1124 void MyPanel::DoCopyToClipboard()
1126 // On X11, we want to get the data from the primary selection instead
1127 // of the normal clipboard (which isn't normal under X11 at all). This
1128 // call has no effect under MSW.
1129 wxTheClipboard
->UsePrimarySelection();
1131 wxString
text( GetFocusedText()->GetStringSelection() );
1136 *m_log
<< _T("No text to copy.\n");
1142 if (!wxTheClipboard
->Open())
1145 *m_log
<< _T("Error opening the clipboard.\n");
1153 *m_log
<< _T("Successfully opened the clipboard.\n");
1157 wxTextDataObject
*data
= new wxTextDataObject( text
);
1159 if (!wxTheClipboard
->SetData( data
))
1162 *m_log
<< _T("Error while copying to the clipboard.\n");
1168 *m_log
<< _T("Successfully copied data to the clipboard.\n");
1172 wxTheClipboard
->Close();
1175 *m_log
<< _T("Closed the clipboard.\n");
1179 #endif // wxUSE_CLIPBOARD
1181 void MyPanel::DoMoveToEndOfText()
1183 m_multitext
->SetInsertionPointEnd();
1184 m_multitext
->SetFocus();
1187 void MyPanel::DoMoveToEndOfEntry()
1189 m_text
->SetInsertionPointEnd();
1193 void MyPanel::DoRemoveText()
1195 GetFocusedText()->Remove(0, 10);
1198 void MyPanel::DoReplaceText()
1200 GetFocusedText()->Replace(3, 8, _T("ABC"));
1203 void MyPanel::DoSelectText()
1205 GetFocusedText()->SetSelection(3, 8);
1208 //----------------------------------------------------------------------
1210 //----------------------------------------------------------------------
1212 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1213 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
1214 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
1215 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
1216 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
1217 EVT_MENU(TEXT_RICH_TEXT_TEST
, MyFrame::OnRichTextTest
)
1219 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
1220 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
1221 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
1222 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
1223 EVT_MENU(TEXT_LOG_FOCUS
,MyFrame::OnLogFocus
)
1225 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
1229 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
1230 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
1231 #endif // wxUSE_TOOLTIPS
1234 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
1235 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
1237 EVT_UPDATE_UI(TEXT_CLIPBOARD_PASTE
, MyFrame::OnUpdatePasteFromClipboard
)
1238 EVT_UPDATE_UI(TEXT_CLIPBOARD_COPY
, MyFrame::OnUpdateCopyToClipboard
)
1239 #endif // wxUSE_CLIPBOARD
1241 EVT_MENU(TEXT_REMOVE
, MyFrame::OnRemoveText
)
1242 EVT_MENU(TEXT_REPLACE
, MyFrame::OnReplaceText
)
1243 EVT_MENU(TEXT_SELECT
, MyFrame::OnSelectText
)
1244 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
1245 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
1246 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
1247 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
1249 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
1250 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
1252 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
1253 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
1254 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
1255 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
1257 EVT_MENU(TEXT_GET_LINE
, MyFrame::OnGetLine
)
1258 EVT_MENU(TEXT_GET_LINELENGTH
, MyFrame::OnGetLineLength
)
1260 EVT_MENU(TEXT_SET
, MyFrame::OnSetText
)
1262 EVT_IDLE(MyFrame::OnIdle
)
1265 MyFrame::MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
)
1266 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
) )
1270 #endif // wxUSE_STATUSBAR
1272 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
1275 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
1280 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1282 wxBeginBusyCursor();
1284 wxMessageDialog
dialog(this,
1285 _T("This is a text control sample. It demonstrates the many different\n")
1286 _T("text control styles, the use of the clipboard, setting and handling\n")
1287 _T("tooltips and intercepting key and char events.\n")
1289 _T("Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin"),
1290 _T("About wxTextCtrl Sample"),
1291 wxOK
| wxICON_INFORMATION
);
1300 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& WXUNUSED(event
))
1302 static long s_delay
= 5000;
1305 delay
.Printf( _T("%ld"), s_delay
);
1307 delay
= wxGetTextFromUser(_T("Enter delay (in milliseconds)"),
1308 _T("Set tooltip delay"),
1312 return; // cancelled
1314 wxSscanf(delay
, _T("%ld"), &s_delay
);
1316 wxToolTip::SetDelay(s_delay
);
1318 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
1321 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
1323 static bool s_enabled
= true;
1325 s_enabled
= !s_enabled
;
1327 wxToolTip::Enable(s_enabled
);
1329 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
1334 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1336 m_panel
->m_log
->Clear();
1340 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1342 static bool s_editable
= true;
1344 s_editable
= !s_editable
;
1345 m_panel
->m_text
->SetEditable(s_editable
);
1346 m_panel
->m_password
->SetEditable(s_editable
);
1347 m_panel
->m_multitext
->SetEditable(s_editable
);
1348 m_panel
->m_textrich
->SetEditable(s_editable
);
1351 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1353 bool enabled
= m_panel
->m_text
->IsEnabled();
1356 m_panel
->m_text
->Enable(enabled
);
1357 m_panel
->m_password
->Enable(enabled
);
1358 m_panel
->m_multitext
->Enable(enabled
);
1359 m_panel
->m_readonly
->Enable(enabled
);
1360 m_panel
->m_limited
->Enable(enabled
);
1361 m_panel
->m_textrich
->Enable(enabled
);
1364 void MyFrame::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1366 if ( m_panel
->m_textrich
->SaveFile(_T("dummy.txt")) )
1369 // verify that the fil length is correct (it wasn't under Win95)
1370 wxFile
file(wxT("dummy.txt"));
1372 _T("Successfully saved file (text len = %lu, file size = %ld)"),
1373 (unsigned long)m_panel
->m_textrich
->GetValue().length(),
1374 (long) file
.Length());
1378 wxLogStatus(this, _T("Couldn't save the file"));
1381 void MyFrame::OnFileLoad(wxCommandEvent
& WXUNUSED(event
))
1383 if ( m_panel
->m_textrich
->LoadFile(_T("dummy.txt")) )
1384 wxLogStatus(this, _T("Successfully loaded file"));
1386 wxLogStatus(this, _T("Couldn't load the file"));
1389 void MyFrame::OnRichTextTest(wxCommandEvent
& WXUNUSED(event
))
1391 RichTextFrame
* frame
= new RichTextFrame(this, _T("Rich Text Editor"));
1395 void MyFrame::OnIdle( wxIdleEvent
& event
)
1397 // track the window which has the focus in the status bar
1398 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1399 wxWindow
*focus
= wxWindow::FindFocus();
1400 if ( focus
&& (focus
!= s_windowFocus
) )
1402 s_windowFocus
= focus
;
1407 _T("Focus: wxWindow = %p, HWND = %08x"),
1409 _T("Focus: wxWindow = %p"),
1413 , (unsigned int) s_windowFocus
->GetHWND()
1419 #endif // wxUSE_STATUSBAR
1425 * RichTextFrame is used to demonstrate rich text behaviour
1430 RICHTEXT_CLOSE
= 1000,
1431 RICHTEXT_LEFT_ALIGN
,
1432 RICHTEXT_RIGHT_ALIGN
,
1435 RICHTEXT_CHANGE_FONT
,
1436 RICHTEXT_CHANGE_TEXT_COLOUR
,
1437 RICHTEXT_CHANGE_BACKGROUND_COLOUR
,
1438 RICHTEXT_LEFT_INDENT
,
1439 RICHTEXT_RIGHT_INDENT
1442 BEGIN_EVENT_TABLE(RichTextFrame
, wxFrame
)
1443 EVT_IDLE(RichTextFrame::OnIdle
)
1444 EVT_MENU(RICHTEXT_CLOSE
, RichTextFrame::OnClose
)
1445 EVT_MENU(RICHTEXT_LEFT_ALIGN
, RichTextFrame::OnLeftAlign
)
1446 EVT_MENU(RICHTEXT_RIGHT_ALIGN
, RichTextFrame::OnRightAlign
)
1447 EVT_MENU(RICHTEXT_CENTRE
, RichTextFrame::OnCentre
)
1448 EVT_MENU(RICHTEXT_JUSTIFY
, RichTextFrame::OnJustify
)
1449 EVT_MENU(RICHTEXT_CHANGE_FONT
, RichTextFrame::OnChangeFont
)
1450 EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR
, RichTextFrame::OnChangeTextColour
)
1451 EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, RichTextFrame::OnChangeBackgroundColour
)
1452 EVT_MENU(RICHTEXT_LEFT_INDENT
, RichTextFrame::OnLeftIndent
)
1453 EVT_MENU(RICHTEXT_RIGHT_INDENT
, RichTextFrame::OnRightIndent
)
1456 RichTextFrame::RichTextFrame(wxWindow
* parent
, const wxString
& title
):
1457 wxFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(300, 400))
1459 m_currentPosition
= -1;
1460 m_textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1461 wxDefaultSize
, wxTE_MULTILINE
|wxTE_RICH2
);
1465 for (i
= 0; i
< 10; i
++)
1468 for (j
= 0; j
< 10; j
++)
1470 value
<< wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
1472 value
<< wxT("\n\n");
1474 m_textCtrl
->SetValue(value
);
1476 wxMenuBar
* menuBar
= new wxMenuBar
;
1477 wxMenu
* fileMenu
= new wxMenu
;
1478 fileMenu
->Append(RICHTEXT_CLOSE
, _("Close\tCtrl+W"));
1479 menuBar
->Append(fileMenu
, _("File"));
1481 wxMenu
* editMenu
= new wxMenu
;
1482 editMenu
->Append(RICHTEXT_LEFT_ALIGN
, _("Left Align"));
1483 editMenu
->Append(RICHTEXT_RIGHT_ALIGN
, _("Right Align"));
1484 editMenu
->Append(RICHTEXT_CENTRE
, _("Centre"));
1485 editMenu
->Append(RICHTEXT_JUSTIFY
, _("Justify"));
1486 editMenu
->AppendSeparator();
1487 editMenu
->Append(RICHTEXT_CHANGE_FONT
, _("Change Font"));
1488 editMenu
->Append(RICHTEXT_CHANGE_TEXT_COLOUR
, _("Change Text Colour"));
1489 editMenu
->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, _("Change Background Colour"));
1490 editMenu
->AppendSeparator();
1491 editMenu
->Append(RICHTEXT_LEFT_INDENT
, _("Left Indent"));
1492 editMenu
->Append(RICHTEXT_RIGHT_INDENT
, _("Right indent"));
1493 menuBar
->Append(editMenu
, _("Edit"));
1495 SetMenuBar(menuBar
);
1498 #endif // wxUSE_STATUSBAR
1503 void RichTextFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
1508 void RichTextFrame::OnLeftAlign(wxCommandEvent
& WXUNUSED(event
))
1511 attr
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
1514 m_textCtrl
->GetSelection(& start
, & end
);
1515 m_textCtrl
->SetStyle(start
, end
, attr
);
1517 m_currentPosition
= -1;
1520 void RichTextFrame::OnRightAlign(wxCommandEvent
& WXUNUSED(event
))
1523 attr
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1526 m_textCtrl
->GetSelection(& start
, & end
);
1527 m_textCtrl
->SetStyle(start
, end
, attr
);
1529 m_currentPosition
= -1;
1532 void RichTextFrame::OnJustify(wxCommandEvent
& WXUNUSED(event
))
1535 attr
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
1538 m_textCtrl
->GetSelection(& start
, & end
);
1539 m_textCtrl
->SetStyle(start
, end
, attr
);
1541 m_currentPosition
= -1;
1544 void RichTextFrame::OnCentre(wxCommandEvent
& WXUNUSED(event
))
1547 attr
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1550 m_textCtrl
->GetSelection(& start
, & end
);
1551 m_textCtrl
->SetStyle(start
, end
, attr
);
1553 m_currentPosition
= -1;
1556 void RichTextFrame::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
1560 wxFontDialog
dialog(this, data
);
1562 if (dialog
.ShowModal() == wxID_OK
)
1564 wxFontData retData
= dialog
.GetFontData();
1565 wxFont font
= retData
.GetChosenFont();
1571 m_textCtrl
->GetSelection(& start
, & end
);
1572 m_textCtrl
->SetStyle(start
, end
, attr
);
1574 m_currentPosition
= -1;
1578 void RichTextFrame::OnChangeTextColour(wxCommandEvent
& WXUNUSED(event
))
1581 data
.SetColour(* wxBLACK
);
1582 data
.SetChooseFull(true);
1583 for (int i
= 0; i
< 16; i
++)
1585 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1586 data
.SetCustomColour(i
, colour
);
1589 wxColourDialog
dialog(this, &data
);
1590 dialog
.SetTitle(_T("Choose the text colour"));
1591 if (dialog
.ShowModal() == wxID_OK
)
1593 wxColourData retData
= dialog
.GetColourData();
1594 wxColour col
= retData
.GetColour();
1597 attr
.SetTextColour(col
);
1600 m_textCtrl
->GetSelection(& start
, & end
);
1601 m_textCtrl
->SetStyle(start
, end
, attr
);
1603 m_currentPosition
= -1;
1607 void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
1610 data
.SetColour(* wxWHITE
);
1611 data
.SetChooseFull(true);
1612 for (int i
= 0; i
< 16; i
++)
1614 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1615 data
.SetCustomColour(i
, colour
);
1618 wxColourDialog
dialog(this, &data
);
1619 dialog
.SetTitle(_T("Choose the text background colour"));
1620 if (dialog
.ShowModal() == wxID_OK
)
1622 wxColourData retData
= dialog
.GetColourData();
1623 wxColour col
= retData
.GetColour();
1626 attr
.SetBackgroundColour(col
);
1629 m_textCtrl
->GetSelection(& start
, & end
);
1630 m_textCtrl
->SetStyle(start
, end
, attr
);
1632 m_currentPosition
= -1;
1636 void RichTextFrame::OnLeftIndent(wxCommandEvent
& WXUNUSED(event
))
1638 wxString indentStr
= wxGetTextFromUser
1640 _("Please enter the left indent in tenths of a millimetre."),
1645 if (!indentStr
.IsEmpty())
1647 int indent
= wxAtoi(indentStr
);
1650 attr
.SetLeftIndent(indent
);
1653 m_textCtrl
->GetSelection(& start
, & end
);
1654 m_textCtrl
->SetStyle(start
, end
, attr
);
1656 m_currentPosition
= -1;
1660 void RichTextFrame::OnRightIndent(wxCommandEvent
& WXUNUSED(event
))
1662 wxString indentStr
= wxGetTextFromUser
1664 _("Please enter the right indent in tenths of a millimetre."),
1669 if (!indentStr
.IsEmpty())
1671 int indent
= wxAtoi(indentStr
);
1674 attr
.SetRightIndent(indent
);
1677 m_textCtrl
->GetSelection(& start
, & end
);
1678 m_textCtrl
->SetStyle(start
, end
, attr
);
1680 m_currentPosition
= -1;
1684 void RichTextFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1686 long insertionPoint
= m_textCtrl
->GetInsertionPoint();
1687 if (insertionPoint
!= m_currentPosition
)
1691 if (m_textCtrl
->GetStyle(insertionPoint
, attr
))
1694 wxString
facename(wxT("unknown"));
1695 if (attr
.GetFont().Ok())
1697 facename
= attr
.GetFont().GetFaceName();
1699 wxString
alignment(wxT("unknown alignment"));
1700 if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1701 alignment
= wxT("centred");
1702 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
1703 alignment
= wxT("right-aligned");
1704 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1705 alignment
= wxT("left-aligned");
1706 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
1707 alignment
= wxT("justified");
1708 msg
.Printf(wxT("Facename: %s, wxColour(%d, %d, %d), %s"),
1709 (const wxChar
*) facename
,
1710 attr
.GetTextColour().Red(), attr
.GetTextColour().Green(), attr
.GetTextColour().Blue(),
1711 (const wxChar
*) alignment
);
1715 if (attr
.GetFont().GetWeight() == wxBOLD
)
1716 msg
+= wxT(" BOLD");
1717 else if (attr
.GetFont().GetWeight() == wxNORMAL
)
1718 msg
+= wxT(" NORMAL");
1720 if (attr
.GetFont().GetStyle() == wxITALIC
)
1721 msg
+= wxT(" ITALIC");
1723 if (attr
.GetFont().GetUnderlined())
1724 msg
+= wxT(" UNDERLINED");
1729 #endif // wxUSE_STATUSBAR
1730 m_currentPosition
= insertionPoint
;