1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: TextCtrl wxWindows sample
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "text.cpp"
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
27 #include "wx/dataobj.h"
28 #include "wx/clipbrd.h"
36 #include "wx/tooltip.h"
39 // We test for wxUSE_DRAG_AND_DROP also, because data objects may not be
40 // implemented for compilers that can't cope with the OLE parts in
41 // wxUSE_DRAG_AND_DROP.
42 #if !wxUSE_DRAG_AND_DROP
43 #undef wxUSE_CLIPBOARD
44 #define wxUSE_CLIPBOARD 0
47 #include "wx/colordlg.h"
48 #include "wx/fontdlg.h"
50 //----------------------------------------------------------------------
52 //----------------------------------------------------------------------
54 class MyApp
: public wxApp
60 // a text ctrl which allows to call different wxTextCtrl functions
61 // interactively by pressing function keys in it
62 class MyTextCtrl
: public wxTextCtrl
65 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
66 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
67 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
)
72 void OnKeyDown(wxKeyEvent
& event
);
73 void OnKeyUp(wxKeyEvent
& event
);
74 void OnChar(wxKeyEvent
& event
);
76 void OnText(wxCommandEvent
& event
);
77 void OnTextURL(wxTextUrlEvent
& event
);
78 void OnTextMaxLen(wxCommandEvent
& event
);
80 void OnMouseEvent(wxMouseEvent
& event
);
82 void OnSetFocus(wxFocusEvent
& event
);
83 void OnKillFocus(wxFocusEvent
& event
);
85 static bool ms_logKey
;
86 static bool ms_logChar
;
87 static bool ms_logMouse
;
88 static bool ms_logText
;
89 static bool ms_logFocus
;
92 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
93 void LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
100 class MyPanel
: public wxPanel
103 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
104 virtual ~MyPanel() { delete wxLog::SetActiveTarget(m_logOld
); }
107 void DoPasteFromClipboard();
108 void DoCopyToClipboard();
109 #endif // wxUSE_CLIPBOARD
112 void DoReplaceText();
114 void DoMoveToEndOfText();
115 void DoMoveToEndOfEntry();
117 void OnSize( wxSizeEvent
&event
);
120 MyTextCtrl
*m_password
;
123 MyTextCtrl
*m_readonly
;
124 MyTextCtrl
*m_limited
;
126 MyTextCtrl
*m_multitext
;
127 MyTextCtrl
*m_horizontal
;
129 MyTextCtrl
*m_textrich
;
136 // get the currently focused text control or return the default one is no
137 // text ctrl has focus
138 wxTextCtrl
*GetFocusedText(wxTextCtrl
*textDef
);
140 DECLARE_EVENT_TABLE()
143 class MyFrame
: public wxFrame
146 MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
148 void OnQuit(wxCommandEvent
& event
);
149 void OnAbout(wxCommandEvent
& event
);
151 void OnSetTooltipDelay(wxCommandEvent
& event
);
152 void OnToggleTooltips(wxCommandEvent
& event
);
153 #endif // wxUSE_TOOLTIPS
156 void OnPasteFromClipboard( wxCommandEvent
&event
)
157 { m_panel
->DoPasteFromClipboard(); }
158 void OnCopyToClipboard( wxCommandEvent
&event
)
159 { m_panel
->DoCopyToClipboard(); }
160 #endif // wxUSE_CLIPBOARD
162 void OnAddTextFreeze( wxCommandEvent
& event
)
164 void OnAddText( wxCommandEvent
& event
)
165 { DoAddText(false); }
166 void OnRemoveText( wxCommandEvent
& event
)
167 { m_panel
->DoRemoveText(); }
168 void OnReplaceText( wxCommandEvent
& event
)
169 { m_panel
->DoReplaceText(); }
170 void OnSelectText( wxCommandEvent
& event
)
171 { m_panel
->DoSelectText(); }
173 void OnMoveToEndOfText( wxCommandEvent
&event
)
174 { m_panel
->DoMoveToEndOfText(); }
175 void OnMoveToEndOfEntry( wxCommandEvent
&event
)
176 { m_panel
->DoMoveToEndOfEntry(); }
178 void OnScrollLineDown(wxCommandEvent
& event
)
180 if ( !m_panel
->m_textrich
->LineDown() )
181 wxLogMessage(_T("Already at the bottom"));
184 void OnScrollLineUp(wxCommandEvent
& event
)
186 if ( !m_panel
->m_textrich
->LineUp() )
187 wxLogMessage(_T("Already at the top"));
190 void OnScrollPageDown(wxCommandEvent
& event
)
192 if ( !m_panel
->m_textrich
->PageDown() )
193 wxLogMessage(_T("Already at the bottom"));
196 void OnScrollPageUp(wxCommandEvent
& event
)
198 if ( !m_panel
->m_textrich
->PageUp() )
199 wxLogMessage(_T("Already at the top"));
202 void OnLogClear(wxCommandEvent
& event
);
203 void OnFileSave(wxCommandEvent
& event
);
204 void OnFileLoad(wxCommandEvent
& event
);
205 void OnRichTextTest(wxCommandEvent
& event
);
207 void OnSetEditable(wxCommandEvent
& event
);
208 void OnSetEnabled(wxCommandEvent
& event
);
210 void OnLogKey(wxCommandEvent
& event
)
212 MyTextCtrl::ms_logKey
= event
.IsChecked();
215 void OnLogChar(wxCommandEvent
& event
)
217 MyTextCtrl::ms_logChar
= event
.IsChecked();
220 void OnLogMouse(wxCommandEvent
& event
)
222 MyTextCtrl::ms_logMouse
= event
.IsChecked();
225 void OnLogText(wxCommandEvent
& event
)
227 MyTextCtrl::ms_logText
= event
.IsChecked();
230 void OnLogFocus(wxCommandEvent
& event
)
232 MyTextCtrl::ms_logFocus
= event
.IsChecked();
235 void OnSetText(wxCommandEvent
& event
)
237 m_panel
->m_text
->SetValue(_T("Hello, world (what else did you expect)?"));
240 void OnIdle( wxIdleEvent
& event
);
243 void DoAddText(bool freeze
)
245 wxTextCtrl
* const text
= m_panel
->m_textrich
;
251 for ( int i
= 0; i
< 100; i
++ )
253 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
259 text
->SetInsertionPoint(0);
264 DECLARE_EVENT_TABLE()
268 * RichTextFrame is used to demonstrate rich text behaviour
271 class RichTextFrame
: public wxFrame
274 RichTextFrame(wxWindow
* parent
, const wxString
& title
);
278 void OnClose(wxCommandEvent
& event
);
279 void OnIdle(wxIdleEvent
& event
);
280 void OnLeftAlign(wxCommandEvent
& event
);
281 void OnRightAlign(wxCommandEvent
& event
);
282 void OnJustify(wxCommandEvent
& event
);
283 void OnCentre(wxCommandEvent
& event
);
284 void OnChangeFont(wxCommandEvent
& event
);
285 void OnChangeTextColour(wxCommandEvent
& event
);
286 void OnChangeBackgroundColour(wxCommandEvent
& event
);
287 void OnLeftIndent(wxCommandEvent
& event
);
288 void OnRightIndent(wxCommandEvent
& event
);
291 wxTextCtrl
*m_textCtrl
;
292 long m_currentPosition
;
294 DECLARE_EVENT_TABLE()
297 //----------------------------------------------------------------------
299 //----------------------------------------------------------------------
303 //----------------------------------------------------------------------
305 //----------------------------------------------------------------------
317 TEXT_CLIPBOARD_COPY
= 200,
318 TEXT_CLIPBOARD_PASTE
,
321 TEXT_TOOLTIPS_SETDELAY
= 300,
322 TEXT_TOOLTIPS_ENABLE
,
352 // Create the main frame window
353 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
354 _T("Text wxWindows sample"), 50, 50, 700, 420);
355 frame
->SetSizeHints( 500, 400 );
357 wxMenu
*file_menu
= new wxMenu
;
358 file_menu
->Append(TEXT_SAVE
, _T("&Save file\tCtrl-S"),
359 _T("Save the text control contents to file"));
360 file_menu
->Append(TEXT_LOAD
, _T("&Load file\tCtrl-O"),
361 _T("Load the sample file into text control"));
362 file_menu
->AppendSeparator();
363 file_menu
->Append(TEXT_RICH_TEXT_TEST
, _T("Show Rich Text Editor"));
364 file_menu
->AppendSeparator();
365 file_menu
->Append(TEXT_ABOUT
, _T("&About\tAlt-A"));
366 file_menu
->AppendSeparator();
367 file_menu
->Append(TEXT_QUIT
, _T("E&xit\tAlt-X"), _T("Quit this sample"));
369 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
370 menu_bar
->Append(file_menu
, _T("&File"));
373 wxMenu
*tooltip_menu
= new wxMenu
;
374 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, _T("Set &delay\tCtrl-D"));
375 tooltip_menu
->AppendSeparator();
376 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, _T("&Toggle tooltips\tCtrl-T"),
377 _T("enable/disable tooltips"), TRUE
);
378 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, TRUE
);
379 menu_bar
->Append(tooltip_menu
, _T("&Tooltips"));
380 #endif // wxUSE_TOOLTIPS
383 wxMenu
*menuClipboard
= new wxMenu
;
384 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, _T("&Copy\tCtrl-C"),
385 _T("Copy the first line to the clipboard"));
386 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, _T("&Paste\tCtrl-V"),
387 _T("Paste from clipboard to the text control"));
388 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
389 #endif // wxUSE_CLIPBOARD
391 wxMenu
*menuText
= new wxMenu
;
392 menuText
->Append(TEXT_ADD_SOME
, _T("&Append some text\tCtrl-A"));
393 menuText
->Append(TEXT_ADD_FREEZE
, _T("&Append text with freeze/thaw\tShift-Ctrl-A"));
394 menuText
->Append(TEXT_REMOVE
, _T("&Remove first 10 characters\tCtrl-Y"));
395 menuText
->Append(TEXT_REPLACE
, _T("&Replace characters 4 to 8 with ABC\tCtrl-R"));
396 menuText
->Append(TEXT_SELECT
, _T("&Select characters 4 to 8\tCtrl-I"));
397 menuText
->Append(TEXT_SET
, _T("&Set the first text zone value\tCtrl-E"));
398 menuText
->AppendSeparator();
399 menuText
->Append(TEXT_MOVE_ENDTEXT
, _T("Move cursor to the end of &text"));
400 menuText
->Append(TEXT_MOVE_ENDENTRY
, _T("Move cursor to the end of &entry"));
401 menuText
->Append(TEXT_SET_EDITABLE
, _T("Toggle &editable state"), _T(""), TRUE
);
402 menuText
->Append(TEXT_SET_ENABLED
, _T("Toggle e&nabled state"), _T(""), TRUE
);
403 menuText
->Check(TEXT_SET_EDITABLE
, TRUE
);
404 menuText
->Check(TEXT_SET_ENABLED
, TRUE
);
405 menuText
->AppendSeparator();
406 menuText
->Append(TEXT_LINE_DOWN
, _T("Scroll text one line down"));
407 menuText
->Append(TEXT_LINE_UP
, _T("Scroll text one line up"));
408 menuText
->Append(TEXT_PAGE_DOWN
, _T("Scroll text one page down"));
409 menuText
->Append(TEXT_PAGE_DOWN
, _T("Scroll text one page up"));
410 menu_bar
->Append(menuText
, _T("Te&xt"));
412 wxMenu
*menuLog
= new wxMenu
;
413 menuLog
->Append(TEXT_LOG_KEY
, _T("Log &key events"), _T(""), TRUE
);
414 menuLog
->Append(TEXT_LOG_CHAR
, _T("Log &char events"), _T(""), TRUE
);
415 menuLog
->Append(TEXT_LOG_MOUSE
, _T("Log &mouse events"), _T(""), TRUE
);
416 menuLog
->Append(TEXT_LOG_TEXT
, _T("Log &text events"), _T(""), TRUE
);
417 menuLog
->Append(TEXT_LOG_FOCUS
, _T("Log &focus events"), _T(""), TRUE
);
418 menuLog
->AppendSeparator();
419 menuLog
->Append(TEXT_CLEAR
, _T("&Clear the log\tCtrl-C"),
420 _T("Clear the log window contents"));
422 // select only the interesting events by default
423 menuLog
->Check(TEXT_LOG_KEY
, TRUE
);
424 menuLog
->Check(TEXT_LOG_CHAR
, TRUE
);
425 menuLog
->Check(TEXT_LOG_TEXT
, TRUE
);
427 MyTextCtrl::ms_logKey
=
428 MyTextCtrl::ms_logChar
=
429 MyTextCtrl::ms_logText
= TRUE
;
430 menu_bar
->Append(menuLog
, _T("&Log"));
432 frame
->SetMenuBar(menu_bar
);
442 //----------------------------------------------------------------------
444 //----------------------------------------------------------------------
446 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
447 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
448 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
449 EVT_CHAR(MyTextCtrl::OnChar
)
451 EVT_TEXT(-1, MyTextCtrl::OnText
)
452 EVT_TEXT_URL(-1, MyTextCtrl::OnTextURL
)
453 EVT_TEXT_MAXLEN(-1, MyTextCtrl::OnTextMaxLen
)
455 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
457 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus
)
458 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus
)
461 bool MyTextCtrl::ms_logKey
= FALSE
;
462 bool MyTextCtrl::ms_logChar
= FALSE
;
463 bool MyTextCtrl::ms_logMouse
= FALSE
;
464 bool MyTextCtrl::ms_logText
= FALSE
;
465 bool MyTextCtrl::ms_logFocus
= FALSE
;
467 void MyTextCtrl::LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const
470 long keycode
= event
.GetKeyCode();
474 case WXK_BACK
: key
= _T("BACK"); break;
475 case WXK_TAB
: key
= _T("TAB"); break;
476 case WXK_RETURN
: key
= _T("RETURN"); break;
477 case WXK_ESCAPE
: key
= _T("ESCAPE"); break;
478 case WXK_SPACE
: key
= _T("SPACE"); break;
479 case WXK_DELETE
: key
= _T("DELETE"); break;
480 case WXK_START
: key
= _T("START"); break;
481 case WXK_LBUTTON
: key
= _T("LBUTTON"); break;
482 case WXK_RBUTTON
: key
= _T("RBUTTON"); break;
483 case WXK_CANCEL
: key
= _T("CANCEL"); break;
484 case WXK_MBUTTON
: key
= _T("MBUTTON"); break;
485 case WXK_CLEAR
: key
= _T("CLEAR"); break;
486 case WXK_SHIFT
: key
= _T("SHIFT"); break;
487 case WXK_ALT
: key
= _T("ALT"); break;
488 case WXK_CONTROL
: key
= _T("CONTROL"); break;
489 case WXK_MENU
: key
= _T("MENU"); break;
490 case WXK_PAUSE
: key
= _T("PAUSE"); break;
491 case WXK_CAPITAL
: key
= _T("CAPITAL"); break;
492 case WXK_PRIOR
: key
= _T("PRIOR"); break;
493 case WXK_NEXT
: key
= _T("NEXT"); break;
494 case WXK_END
: key
= _T("END"); break;
495 case WXK_HOME
: key
= _T("HOME"); break;
496 case WXK_LEFT
: key
= _T("LEFT"); break;
497 case WXK_UP
: key
= _T("UP"); break;
498 case WXK_RIGHT
: key
= _T("RIGHT"); break;
499 case WXK_DOWN
: key
= _T("DOWN"); break;
500 case WXK_SELECT
: key
= _T("SELECT"); break;
501 case WXK_PRINT
: key
= _T("PRINT"); break;
502 case WXK_EXECUTE
: key
= _T("EXECUTE"); break;
503 case WXK_SNAPSHOT
: key
= _T("SNAPSHOT"); break;
504 case WXK_INSERT
: key
= _T("INSERT"); break;
505 case WXK_HELP
: key
= _T("HELP"); break;
506 case WXK_NUMPAD0
: key
= _T("NUMPAD0"); break;
507 case WXK_NUMPAD1
: key
= _T("NUMPAD1"); break;
508 case WXK_NUMPAD2
: key
= _T("NUMPAD2"); break;
509 case WXK_NUMPAD3
: key
= _T("NUMPAD3"); break;
510 case WXK_NUMPAD4
: key
= _T("NUMPAD4"); break;
511 case WXK_NUMPAD5
: key
= _T("NUMPAD5"); break;
512 case WXK_NUMPAD6
: key
= _T("NUMPAD6"); break;
513 case WXK_NUMPAD7
: key
= _T("NUMPAD7"); break;
514 case WXK_NUMPAD8
: key
= _T("NUMPAD8"); break;
515 case WXK_NUMPAD9
: key
= _T("NUMPAD9"); break;
516 case WXK_MULTIPLY
: key
= _T("MULTIPLY"); break;
517 case WXK_ADD
: key
= _T("ADD"); break;
518 case WXK_SEPARATOR
: key
= _T("SEPARATOR"); break;
519 case WXK_SUBTRACT
: key
= _T("SUBTRACT"); break;
520 case WXK_DECIMAL
: key
= _T("DECIMAL"); break;
521 case WXK_DIVIDE
: key
= _T("DIVIDE"); break;
522 case WXK_F1
: key
= _T("F1"); break;
523 case WXK_F2
: key
= _T("F2"); break;
524 case WXK_F3
: key
= _T("F3"); break;
525 case WXK_F4
: key
= _T("F4"); break;
526 case WXK_F5
: key
= _T("F5"); break;
527 case WXK_F6
: key
= _T("F6"); break;
528 case WXK_F7
: key
= _T("F7"); break;
529 case WXK_F8
: key
= _T("F8"); break;
530 case WXK_F9
: key
= _T("F9"); break;
531 case WXK_F10
: key
= _T("F10"); break;
532 case WXK_F11
: key
= _T("F11"); break;
533 case WXK_F12
: key
= _T("F12"); break;
534 case WXK_F13
: key
= _T("F13"); break;
535 case WXK_F14
: key
= _T("F14"); break;
536 case WXK_F15
: key
= _T("F15"); break;
537 case WXK_F16
: key
= _T("F16"); break;
538 case WXK_F17
: key
= _T("F17"); break;
539 case WXK_F18
: key
= _T("F18"); break;
540 case WXK_F19
: key
= _T("F19"); break;
541 case WXK_F20
: key
= _T("F20"); break;
542 case WXK_F21
: key
= _T("F21"); break;
543 case WXK_F22
: key
= _T("F22"); break;
544 case WXK_F23
: key
= _T("F23"); break;
545 case WXK_F24
: key
= _T("F24"); break;
546 case WXK_NUMLOCK
: key
= _T("NUMLOCK"); break;
547 case WXK_SCROLL
: key
= _T("SCROLL"); break;
548 case WXK_PAGEUP
: key
= _T("PAGEUP"); break;
549 case WXK_PAGEDOWN
: key
= _T("PAGEDOWN"); break;
550 case WXK_NUMPAD_SPACE
: key
= _T("NUMPAD_SPACE"); break;
551 case WXK_NUMPAD_TAB
: key
= _T("NUMPAD_TAB"); break;
552 case WXK_NUMPAD_ENTER
: key
= _T("NUMPAD_ENTER"); break;
553 case WXK_NUMPAD_F1
: key
= _T("NUMPAD_F1"); break;
554 case WXK_NUMPAD_F2
: key
= _T("NUMPAD_F2"); break;
555 case WXK_NUMPAD_F3
: key
= _T("NUMPAD_F3"); break;
556 case WXK_NUMPAD_F4
: key
= _T("NUMPAD_F4"); break;
557 case WXK_NUMPAD_HOME
: key
= _T("NUMPAD_HOME"); break;
558 case WXK_NUMPAD_LEFT
: key
= _T("NUMPAD_LEFT"); break;
559 case WXK_NUMPAD_UP
: key
= _T("NUMPAD_UP"); break;
560 case WXK_NUMPAD_RIGHT
: key
= _T("NUMPAD_RIGHT"); break;
561 case WXK_NUMPAD_DOWN
: key
= _T("NUMPAD_DOWN"); break;
562 case WXK_NUMPAD_PRIOR
: key
= _T("NUMPAD_PRIOR"); break;
563 case WXK_NUMPAD_PAGEUP
: key
= _T("NUMPAD_PAGEUP"); break;
564 case WXK_NUMPAD_PAGEDOWN
: key
= _T("NUMPAD_PAGEDOWN"); break;
565 case WXK_NUMPAD_END
: key
= _T("NUMPAD_END"); break;
566 case WXK_NUMPAD_BEGIN
: key
= _T("NUMPAD_BEGIN"); break;
567 case WXK_NUMPAD_INSERT
: key
= _T("NUMPAD_INSERT"); break;
568 case WXK_NUMPAD_DELETE
: key
= _T("NUMPAD_DELETE"); break;
569 case WXK_NUMPAD_EQUAL
: key
= _T("NUMPAD_EQUAL"); break;
570 case WXK_NUMPAD_MULTIPLY
: key
= _T("NUMPAD_MULTIPLY"); break;
571 case WXK_NUMPAD_ADD
: key
= _T("NUMPAD_ADD"); break;
572 case WXK_NUMPAD_SEPARATOR
: key
= _T("NUMPAD_SEPARATOR"); break;
573 case WXK_NUMPAD_SUBTRACT
: key
= _T("NUMPAD_SUBTRACT"); break;
574 case WXK_NUMPAD_DECIMAL
: key
= _T("NUMPAD_DECIMAL"); break;
578 if ( wxIsprint((int)keycode
) )
579 key
.Printf(_T("'%c'"), (char)keycode
);
580 else if ( keycode
> 0 && keycode
< 27 )
581 key
.Printf(_("Ctrl-%c"), _T('A') + keycode
- 1);
583 key
.Printf(_T("unknown (%ld)"), keycode
);
588 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
591 GetChar( event
.ControlDown(), _T('C') ),
592 GetChar( event
.AltDown(), _T('A') ),
593 GetChar( event
.ShiftDown(), _T('S') ),
594 GetChar( event
.MetaDown(), _T('M') ) );
597 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
602 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
605 dbl
= ev
.LeftDClick();
608 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
610 button
= _T("Middle");
611 dbl
= ev
.MiddleDClick();
614 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
616 button
= _T("Right");
617 dbl
= ev
.RightDClick();
622 return _T("Unknown mouse event");
625 return wxString::Format(_T("%s mouse button %s"),
627 dbl
? _T("double clicked")
628 : up
? _T("released") : _T("clicked"));
631 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
643 msg
= _T("Mouse entered the window");
645 else if ( ev
.Leaving() )
647 msg
= _T("Mouse left the window");
651 msg
= GetMouseEventDesc(ev
);
654 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ")
656 << GetChar( ev
.LeftIsDown(), _T('1') )
657 << GetChar( ev
.MiddleIsDown(), _T('2') )
658 << GetChar( ev
.RightIsDown(), _T('3') )
659 << GetChar( ev
.ControlDown(), _T('C') )
660 << GetChar( ev
.AltDown(), _T('A') )
661 << GetChar( ev
.ShiftDown(), _T('S') )
662 << GetChar( ev
.MetaDown(), _T('M') );
666 //else: we're not interested in mouse move events
669 void MyTextCtrl::OnSetFocus(wxFocusEvent
& event
)
672 wxLogMessage( wxT("%p got focus."), this);
677 void MyTextCtrl::OnKillFocus(wxFocusEvent
& event
)
680 wxLogMessage( wxT("%p lost focus"), this);
685 void MyTextCtrl::OnText(wxCommandEvent
& event
)
690 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
691 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
694 wxLogMessage(_T("Text changed in control '%s'"), data
);
698 // wxLogMessage( event.GetString() );
699 wxLogMessage(_T("Text changed in some control"));
703 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& event
)
705 wxLogMessage(_T("You can't enter more characters into this control."));
708 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
710 const wxMouseEvent
& ev
= event
.GetMouseEvent();
712 // filter out mouse moves, too many of them
716 long start
= event
.GetURLStart(),
717 end
= event
.GetURLEnd();
719 wxLogMessage(_T("Mouse event over URL '%s': %s"),
720 GetValue().Mid(start
, end
- start
).c_str(),
721 GetMouseEventDesc(ev
).c_str());
724 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
727 LogKeyEvent( _T("Char"), event
);
732 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
735 LogKeyEvent( _T("Key up"), event
);
740 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
742 switch ( event
.GetKeyCode() )
745 // show current position and text length
747 long line
, column
, pos
= GetInsertionPoint();
748 PositionToXY(pos
, &column
, &line
);
750 wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
753 (long) GetNumberOfLines(),
754 (long) GetLineLength(line
),
759 GetSelection(&from
, &to
);
761 wxString sel
= GetStringSelection();
763 wxLogMessage(_T("Selection: from %ld to %ld."), from
, to
);
764 wxLogMessage(_T("Selection = '%s' (len = %u)"),
765 sel
.c_str(), sel
.length());
771 SetInsertionPointEnd();
776 SetInsertionPoint(10);
782 wxLogDebug( wxT("Now capturing mouse and events.") );
788 wxLogDebug( wxT("Stopped capturing mouse and events.") );
789 m_hasCapture
= FALSE
;
795 // insert a blank line
800 wxLogMessage(_T("IsModified() before SetValue(): %d"),
802 SetValue(_T("SetValue() has been called"));
803 wxLogMessage(_T("IsModified() after SetValue(): %d"),
808 wxLogMessage(_T("Position 10 should be now visible."));
813 wxLogMessage(_T("Control has been cleared"));
818 WriteText(_T("WriteText() has been called"));
822 AppendText(_T("AppendText() has been called"));
827 LogKeyEvent( wxT("Key down"), event
);
832 //----------------------------------------------------------------------
834 //----------------------------------------------------------------------
836 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
837 EVT_SIZE(MyPanel::OnSize
)
840 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
841 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
843 m_log
= new wxTextCtrl( this, -1, _T("This is the log window.\n"),
844 wxPoint(5,260), wxSize(630,100),
845 wxTE_MULTILINE
| wxTE_READONLY
/* | wxTE_RICH */);
847 m_logOld
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
849 // single line text controls
851 m_text
= new MyTextCtrl( this, -1, _T("Single line."),
852 wxPoint(10,10), wxSize(140,-1),
854 m_text
->SetForegroundColour(*wxBLUE
);
855 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
856 (*m_text
) << _T(" Appended.");
857 m_text
->SetInsertionPoint(0);
858 m_text
->WriteText( _T("Prepended. ") );
860 m_password
= new MyTextCtrl( this, -1, _T(""),
861 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
863 m_readonly
= new MyTextCtrl( this, -1, _T("Read only"),
864 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
866 m_limited
= new MyTextCtrl(this, -1, _T("Max 8 ch"),
867 wxPoint(10, 130), wxSize(140, -1));
868 m_limited
->SetMaxLength(8);
870 // multi line text controls
872 m_horizontal
= new MyTextCtrl( this, -1, _T("Multiline text control with a horizontal scrollbar."),
873 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
875 // a little hack to use the command line argument for encoding testing
876 if ( wxTheApp
->argc
== 2 )
878 switch ( wxTheApp
->argv
[1][0] )
881 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
883 wxFONTENCODING_ISO8859_2
));
884 m_horizontal
->SetValue(_T("®lu»ouèký kùò zbìsile èe¹tina «»"));
888 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
890 wxFONTENCODING_CP1251
));
891 m_horizontal
->SetValue(_T("Ïðèâåò!"));
895 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
897 wxFONTENCODING_CP1251
));
899 m_horizontal
->SetValue(L
"\x0412\x0430\x0434\x0438\x043c \x0426");
901 m_horizontal
->SetValue("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
907 m_horizontal
->SetValue(_T("Text in default encoding"));
910 m_multitext
= new MyTextCtrl( this, -1, _T("Multi line."),
911 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
912 m_multitext
->SetFont(*wxITALIC_FONT
);
913 (*m_multitext
) << _T(" Appended.");
914 m_multitext
->SetInsertionPoint(0);
915 m_multitext
->WriteText( _T("Prepended. ") );
916 m_multitext
->SetForegroundColour(*wxRED
);
917 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
920 m_multitext
->SetToolTip(_T("Press Fn function keys here"));
923 m_tab
= new MyTextCtrl( this, 100, _T("Multiline, allow <TAB> processing."),
924 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
925 m_tab
->SetClientData((void *)_T("tab"));
927 m_enter
= new MyTextCtrl( this, 100, _T("Multiline, allow <ENTER> processing."),
928 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
929 m_enter
->SetClientData((void *)_T("enter"));
932 m_textrich
= new MyTextCtrl(this, -1, _T("Allows more than 30Kb of text\n")
933 _T("(even under broken Win9x)\n")
934 _T("and a very very very very very ")
935 _T("very very very long line to test ")
936 _T("wxHSCROLL style"),
937 wxPoint(450, 10), wxSize(230, 230),
942 m_textrich
->SetStyle(0, 10, *wxRED
);
943 m_textrich
->SetStyle(10, 20, *wxBLUE
);
944 m_textrich
->SetStyle(30, 40,
945 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
946 m_textrich
->SetDefaultStyle(wxTextAttr());
947 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
948 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
949 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
950 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
951 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
952 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
953 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
, *wxWHITE
));
954 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
955 _T("type should be in blue as well"));
957 m_textrich
= new MyTextCtrl(this, -1, _T(""),
958 wxPoint(450, 10), wxSize(230, 230),
965 void MyPanel::OnSize( wxSizeEvent
&event
)
967 wxSize
client_area( GetClientSize() );
968 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
972 wxTextCtrl
*MyPanel::GetFocusedText(wxTextCtrl
*textDef
)
974 wxWindow
*win
= FindFocus();
976 wxTextCtrl
*text
= win
? wxDynamicCast(win
, wxTextCtrl
) : NULL
;
977 return text
? text
: textDef
;
981 void MyPanel::DoPasteFromClipboard()
983 // On X11, we want to get the data from the primary selection instead
984 // of the normal clipboard (which isn't normal under X11 at all). This
985 // call has no effect under MSW.
986 wxTheClipboard
->UsePrimarySelection();
988 if (!wxTheClipboard
->Open())
990 *m_log
<< _T("Error opening the clipboard.\n");
995 *m_log
<< _T("Successfully opened the clipboard.\n");
998 wxTextDataObject data
;
1000 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
1002 *m_log
<< _T("Clipboard supports requested format.\n");
1004 if (wxTheClipboard
->GetData( data
))
1006 *m_log
<< _T("Successfully retrieved data from the clipboard.\n");
1007 *m_multitext
<< data
.GetText() << _T("\n");
1011 *m_log
<< _T("Error getting data from the clipboard.\n");
1016 *m_log
<< _T("Clipboard doesn't support requested format.\n");
1019 wxTheClipboard
->Close();
1021 *m_log
<< _T("Closed the clipboard.\n");
1024 void MyPanel::DoCopyToClipboard()
1026 // On X11, we want to get the data from the primary selection instead
1027 // of the normal clipboard (which isn't normal under X11 at all). This
1028 // call has no effect under MSW.
1029 wxTheClipboard
->UsePrimarySelection();
1031 wxString
text( m_multitext
->GetLineText(0) );
1035 *m_log
<< _T("No text to copy.\n");
1040 if (!wxTheClipboard
->Open())
1042 *m_log
<< _T("Error opening the clipboard.\n");
1048 *m_log
<< _T("Successfully opened the clipboard.\n");
1051 wxTextDataObject
*data
= new wxTextDataObject( text
);
1053 if (!wxTheClipboard
->SetData( data
))
1055 *m_log
<< _T("Error while copying to the clipboard.\n");
1059 *m_log
<< _T("Successfully copied data to the clipboard.\n");
1062 wxTheClipboard
->Close();
1064 *m_log
<< _T("Closed the clipboard.\n");
1067 #endif // wxUSE_CLIPBOARD
1069 void MyPanel::DoMoveToEndOfText()
1071 m_multitext
->SetInsertionPointEnd();
1072 m_multitext
->SetFocus();
1075 void MyPanel::DoMoveToEndOfEntry()
1077 m_text
->SetInsertionPointEnd();
1081 void MyPanel::DoRemoveText()
1083 GetFocusedText(m_multitext
)->Remove(0, 10);
1086 void MyPanel::DoReplaceText()
1088 GetFocusedText(m_multitext
)->Replace(3, 8, _T("ABC"));
1091 void MyPanel::DoSelectText()
1093 GetFocusedText(m_multitext
)->SetSelection(3, 8);
1096 //----------------------------------------------------------------------
1098 //----------------------------------------------------------------------
1100 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1101 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
1102 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
1103 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
1104 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
1105 EVT_MENU(TEXT_RICH_TEXT_TEST
, MyFrame::OnRichTextTest
)
1107 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
1108 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
1109 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
1110 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
1111 EVT_MENU(TEXT_LOG_FOCUS
,MyFrame::OnLogFocus
)
1112 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
1115 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
1116 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
1117 #endif // wxUSE_TOOLTIPS
1120 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
1121 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
1122 #endif // wxUSE_CLIPBOARD
1124 EVT_MENU(TEXT_REMOVE
, MyFrame::OnRemoveText
)
1125 EVT_MENU(TEXT_REPLACE
, MyFrame::OnReplaceText
)
1126 EVT_MENU(TEXT_SELECT
, MyFrame::OnSelectText
)
1127 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
1128 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
1129 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
1130 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
1132 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
1133 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
1135 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
1136 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
1137 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
1138 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
1140 EVT_MENU(TEXT_SET
, MyFrame::OnSetText
)
1142 EVT_IDLE(MyFrame::OnIdle
)
1145 MyFrame::MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
)
1146 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
) )
1150 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
1153 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
1158 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1160 wxBeginBusyCursor();
1162 wxMessageDialog
dialog(this,
1163 _T("This is a text control sample. It demonstrates the many different\n")
1164 _T("text control styles, the use of the clipboard, setting and handling\n")
1165 _T("tooltips and intercepting key and char events.\n")
1167 _T("Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin"),
1168 _T("About wxTextCtrl Sample"),
1169 wxOK
| wxICON_INFORMATION
);
1177 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
1179 static long s_delay
= 5000;
1182 delay
.Printf( _T("%ld"), s_delay
);
1184 delay
= wxGetTextFromUser(_T("Enter delay (in milliseconds)"),
1185 _T("Set tooltip delay"),
1189 return; // cancelled
1191 wxSscanf(delay
, _T("%ld"), &s_delay
);
1193 wxToolTip::SetDelay(s_delay
);
1195 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
1198 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
1200 static bool s_enabled
= TRUE
;
1202 s_enabled
= !s_enabled
;
1204 wxToolTip::Enable(s_enabled
);
1206 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
1210 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1212 m_panel
->m_log
->Clear();
1215 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1217 static bool s_editable
= TRUE
;
1219 s_editable
= !s_editable
;
1220 m_panel
->m_text
->SetEditable(s_editable
);
1221 m_panel
->m_password
->SetEditable(s_editable
);
1222 m_panel
->m_multitext
->SetEditable(s_editable
);
1223 m_panel
->m_textrich
->SetEditable(s_editable
);
1226 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1228 bool enabled
= m_panel
->m_text
->IsEnabled();
1231 m_panel
->m_text
->Enable(enabled
);
1232 m_panel
->m_password
->Enable(enabled
);
1233 m_panel
->m_multitext
->Enable(enabled
);
1234 m_panel
->m_readonly
->Enable(enabled
);
1235 m_panel
->m_limited
->Enable(enabled
);
1236 m_panel
->m_textrich
->Enable(enabled
);
1239 void MyFrame::OnFileSave(wxCommandEvent
& event
)
1241 if ( m_panel
->m_textrich
->SaveFile(_T("dummy.txt")) )
1244 // verify that the fil length is correct (it wasn't under Win95)
1245 wxFile
file(wxT("dummy.txt"));
1247 _T("Successfully saved file (text len = %lu, file size = %ld)"),
1248 (unsigned long)m_panel
->m_textrich
->GetValue().length(),
1249 (long) file
.Length());
1253 wxLogStatus(this, _T("Couldn't save the file"));
1256 void MyFrame::OnFileLoad(wxCommandEvent
& event
)
1258 if ( m_panel
->m_textrich
->LoadFile(_T("dummy.txt")) )
1259 wxLogStatus(this, _T("Successfully loaded file"));
1261 wxLogStatus(this, _T("Couldn't load the file"));
1264 void MyFrame::OnRichTextTest(wxCommandEvent
& event
)
1266 RichTextFrame
* frame
= new RichTextFrame(this, _T("Rich Text Editor"));
1270 void MyFrame::OnIdle( wxIdleEvent
& event
)
1272 // track the window which has the focus in the status bar
1273 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1274 wxWindow
*focus
= wxWindow::FindFocus();
1275 if ( focus
&& (focus
!= s_windowFocus
) )
1277 s_windowFocus
= focus
;
1282 _T("Focus: wxWindow = %p, HWND = %08x"),
1284 _T("Focus: wxWindow = %p"),
1288 , (unsigned int) s_windowFocus
->GetHWND()
1298 * RichTextFrame is used to demonstrate rich text behaviour
1303 RICHTEXT_CLOSE
= 1000,
1304 RICHTEXT_LEFT_ALIGN
,
1305 RICHTEXT_RIGHT_ALIGN
,
1308 RICHTEXT_CHANGE_FONT
,
1309 RICHTEXT_CHANGE_TEXT_COLOUR
,
1310 RICHTEXT_CHANGE_BACKGROUND_COLOUR
,
1311 RICHTEXT_LEFT_INDENT
,
1312 RICHTEXT_RIGHT_INDENT
1315 BEGIN_EVENT_TABLE(RichTextFrame
, wxFrame
)
1316 EVT_IDLE(RichTextFrame::OnIdle
)
1317 EVT_MENU(RICHTEXT_CLOSE
, RichTextFrame::OnClose
)
1318 EVT_MENU(RICHTEXT_LEFT_ALIGN
, RichTextFrame::OnLeftAlign
)
1319 EVT_MENU(RICHTEXT_RIGHT_ALIGN
, RichTextFrame::OnRightAlign
)
1320 EVT_MENU(RICHTEXT_CENTRE
, RichTextFrame::OnCentre
)
1321 EVT_MENU(RICHTEXT_JUSTIFY
, RichTextFrame::OnJustify
)
1322 EVT_MENU(RICHTEXT_CHANGE_FONT
, RichTextFrame::OnChangeFont
)
1323 EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR
, RichTextFrame::OnChangeTextColour
)
1324 EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, RichTextFrame::OnChangeBackgroundColour
)
1325 EVT_MENU(RICHTEXT_LEFT_INDENT
, RichTextFrame::OnLeftIndent
)
1326 EVT_MENU(RICHTEXT_RIGHT_INDENT
, RichTextFrame::OnRightIndent
)
1329 RichTextFrame::RichTextFrame(wxWindow
* parent
, const wxString
& title
):
1330 wxFrame(parent
, -1, title
, wxDefaultPosition
, wxSize(300, 400))
1332 m_currentPosition
= -1;
1333 m_textCtrl
= new wxTextCtrl(this, -1, wxEmptyString
, wxDefaultPosition
,
1334 wxDefaultSize
, wxTE_MULTILINE
|wxTE_RICH2
);
1338 for (i
= 0; i
< 10; i
++)
1341 for (j
= 0; j
< 10; j
++)
1343 value
<< wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
1345 value
<< wxT("\n\n");
1347 m_textCtrl
->SetValue(value
);
1349 wxMenuBar
* menuBar
= new wxMenuBar
;
1350 wxMenu
* fileMenu
= new wxMenu
;
1351 fileMenu
->Append(RICHTEXT_CLOSE
, _("Close\tCtrl+W"));
1352 menuBar
->Append(fileMenu
, _("File"));
1354 wxMenu
* editMenu
= new wxMenu
;
1355 editMenu
->Append(RICHTEXT_LEFT_ALIGN
, _("Left Align"));
1356 editMenu
->Append(RICHTEXT_RIGHT_ALIGN
, _("Right Align"));
1357 editMenu
->Append(RICHTEXT_CENTRE
, _("Centre"));
1358 editMenu
->Append(RICHTEXT_JUSTIFY
, _("Justify"));
1359 editMenu
->AppendSeparator();
1360 editMenu
->Append(RICHTEXT_CHANGE_FONT
, _("Change Font"));
1361 editMenu
->Append(RICHTEXT_CHANGE_TEXT_COLOUR
, _("Change Text Colour"));
1362 editMenu
->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, _("Change Background Colour"));
1363 editMenu
->AppendSeparator();
1364 editMenu
->Append(RICHTEXT_LEFT_INDENT
, _("Left Indent"));
1365 editMenu
->Append(RICHTEXT_RIGHT_INDENT
, _("Right indent"));
1366 menuBar
->Append(editMenu
, _("Edit"));
1368 SetMenuBar(menuBar
);
1374 void RichTextFrame::OnClose(wxCommandEvent
& event
)
1379 void RichTextFrame::OnLeftAlign(wxCommandEvent
& event
)
1382 attr
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
1385 m_textCtrl
->GetSelection(& start
, & end
);
1386 m_textCtrl
->SetStyle(start
, end
, attr
);
1388 m_currentPosition
= -1;
1391 void RichTextFrame::OnRightAlign(wxCommandEvent
& event
)
1394 attr
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1397 m_textCtrl
->GetSelection(& start
, & end
);
1398 m_textCtrl
->SetStyle(start
, end
, attr
);
1400 m_currentPosition
= -1;
1403 void RichTextFrame::OnJustify(wxCommandEvent
& event
)
1406 attr
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
1409 m_textCtrl
->GetSelection(& start
, & end
);
1410 m_textCtrl
->SetStyle(start
, end
, attr
);
1412 m_currentPosition
= -1;
1415 void RichTextFrame::OnCentre(wxCommandEvent
& event
)
1418 attr
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1421 m_textCtrl
->GetSelection(& start
, & end
);
1422 m_textCtrl
->SetStyle(start
, end
, attr
);
1424 m_currentPosition
= -1;
1427 void RichTextFrame::OnChangeFont(wxCommandEvent
& event
)
1431 wxFontDialog
dialog(this, data
);
1433 if (dialog
.ShowModal() == wxID_OK
)
1435 wxFontData retData
= dialog
.GetFontData();
1436 wxFont font
= retData
.GetChosenFont();
1442 m_textCtrl
->GetSelection(& start
, & end
);
1443 m_textCtrl
->SetStyle(start
, end
, attr
);
1445 m_currentPosition
= -1;
1449 void RichTextFrame::OnChangeTextColour(wxCommandEvent
& event
)
1452 data
.SetColour(* wxBLACK
);
1453 data
.SetChooseFull(TRUE
);
1454 for (int i
= 0; i
< 16; i
++)
1456 wxColour
colour(i
*16, i
*16, i
*16);
1457 data
.SetCustomColour(i
, colour
);
1460 wxColourDialog
dialog(this, &data
);
1461 dialog
.SetTitle(_T("Choose the text colour"));
1462 if (dialog
.ShowModal() == wxID_OK
)
1464 wxColourData retData
= dialog
.GetColourData();
1465 wxColour col
= retData
.GetColour();
1468 attr
.SetTextColour(col
);
1471 m_textCtrl
->GetSelection(& start
, & end
);
1472 m_textCtrl
->SetStyle(start
, end
, attr
);
1474 m_currentPosition
= -1;
1478 void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent
& event
)
1481 data
.SetColour(* wxWHITE
);
1482 data
.SetChooseFull(TRUE
);
1483 for (int i
= 0; i
< 16; i
++)
1485 wxColour
colour(i
*16, i
*16, i
*16);
1486 data
.SetCustomColour(i
, colour
);
1489 wxColourDialog
dialog(this, &data
);
1490 dialog
.SetTitle(_T("Choose the text background colour"));
1491 if (dialog
.ShowModal() == wxID_OK
)
1493 wxColourData retData
= dialog
.GetColourData();
1494 wxColour col
= retData
.GetColour();
1497 attr
.SetBackgroundColour(col
);
1500 m_textCtrl
->GetSelection(& start
, & end
);
1501 m_textCtrl
->SetStyle(start
, end
, attr
);
1503 m_currentPosition
= -1;
1507 void RichTextFrame::OnLeftIndent(wxCommandEvent
& event
)
1509 wxString indentStr
= wxGetTextFromUser(_("Please enter the left indent in tenths of a millimetre."),
1511 if (!indentStr
.IsEmpty())
1513 int indent
= wxAtoi(indentStr
);
1516 attr
.SetLeftIndent(indent
);
1519 m_textCtrl
->GetSelection(& start
, & end
);
1520 m_textCtrl
->SetStyle(start
, end
, attr
);
1522 m_currentPosition
= -1;
1526 void RichTextFrame::OnRightIndent(wxCommandEvent
& event
)
1528 wxString indentStr
= wxGetTextFromUser(_("Please enter the right indent in tenths of a millimetre."),
1530 if (!indentStr
.IsEmpty())
1532 int indent
= wxAtoi(indentStr
);
1535 attr
.SetRightIndent(indent
);
1538 m_textCtrl
->GetSelection(& start
, & end
);
1539 m_textCtrl
->SetStyle(start
, end
, attr
);
1541 m_currentPosition
= -1;
1545 void RichTextFrame::OnIdle(wxIdleEvent
& event
)
1547 long insertionPoint
= m_textCtrl
->GetInsertionPoint();
1548 if (insertionPoint
!= m_currentPosition
)
1551 if (m_textCtrl
->GetStyle(insertionPoint
, attr
))
1554 wxString
facename(wxT("unknown"));
1555 if (attr
.GetFont().Ok())
1557 facename
= attr
.GetFont().GetFaceName();
1559 wxString
alignment(wxT("unknown alignment"));
1560 if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1561 alignment
= wxT("centred");
1562 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
1563 alignment
= wxT("right-aligned");
1564 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1565 alignment
= wxT("left-aligned");
1566 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
1567 alignment
= wxT("justified");
1568 msg
.Printf(wxT("Facename: %s, wxColour(%d, %d, %d), %s"),
1569 (const wxChar
*) facename
,
1570 attr
.GetTextColour().Red(), attr
.GetTextColour().Green(), attr
.GetTextColour().Blue(),
1571 (const wxChar
*) alignment
);
1574 m_currentPosition
= insertionPoint
;