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"
46 //----------------------------------------------------------------------
48 //----------------------------------------------------------------------
50 class MyApp
: public wxApp
56 // a text ctrl which allows to call different wxTextCtrl functions
57 // interactively by pressing function keys in it
58 class MyTextCtrl
: public wxTextCtrl
61 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
62 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
63 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
)
68 void OnKeyDown(wxKeyEvent
& event
);
69 void OnKeyUp(wxKeyEvent
& event
);
70 void OnChar(wxKeyEvent
& event
);
72 void OnText(wxCommandEvent
& event
);
73 void OnTextURL(wxTextUrlEvent
& event
);
74 void OnTextMaxLen(wxCommandEvent
& event
);
76 void OnMouseEvent(wxMouseEvent
& event
);
78 void OnSetFocus(wxFocusEvent
& event
);
79 void OnKillFocus(wxFocusEvent
& event
);
81 static bool ms_logKey
;
82 static bool ms_logChar
;
83 static bool ms_logMouse
;
84 static bool ms_logText
;
85 static bool ms_logFocus
;
88 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
89 void LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
96 class MyPanel
: public wxPanel
99 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
103 delete wxLog::SetActiveTarget(m_logOld
);
108 void DoPasteFromClipboard();
109 void DoCopyToClipboard();
110 #endif // wxUSE_CLIPBOARD
113 void DoReplaceText();
115 void DoMoveToEndOfText();
116 void DoMoveToEndOfEntry();
118 void OnSize( wxSizeEvent
&event
);
121 MyTextCtrl
*m_password
;
124 MyTextCtrl
*m_readonly
;
125 MyTextCtrl
*m_limited
;
127 MyTextCtrl
*m_multitext
;
128 MyTextCtrl
*m_horizontal
;
130 MyTextCtrl
*m_textrich
;
138 // get the currently focused text control or return the default one is no
139 // text ctrl has focus
140 wxTextCtrl
*GetFocusedText(wxTextCtrl
*textDef
);
142 DECLARE_EVENT_TABLE()
145 class MyFrame
: public wxFrame
148 MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
);
150 void OnQuit(wxCommandEvent
& event
);
151 void OnAbout(wxCommandEvent
& event
);
153 void OnSetTooltipDelay(wxCommandEvent
& event
);
154 void OnToggleTooltips(wxCommandEvent
& event
);
155 #endif // wxUSE_TOOLTIPS
158 void OnPasteFromClipboard( wxCommandEvent
& WXUNUSED(event
) )
159 { m_panel
->DoPasteFromClipboard(); }
160 void OnCopyToClipboard( wxCommandEvent
& WXUNUSED(event
) )
161 { m_panel
->DoCopyToClipboard(); }
162 #endif // wxUSE_CLIPBOARD
164 void OnAddTextFreeze( wxCommandEvent
& WXUNUSED(event
) )
166 void OnAddText( wxCommandEvent
& WXUNUSED(event
) )
167 { DoAddText(false); }
168 void OnRemoveText( wxCommandEvent
& WXUNUSED(event
) )
169 { m_panel
->DoRemoveText(); }
170 void OnReplaceText( wxCommandEvent
& WXUNUSED(event
) )
171 { m_panel
->DoReplaceText(); }
172 void OnSelectText( wxCommandEvent
& WXUNUSED(event
) )
173 { m_panel
->DoSelectText(); }
175 void OnMoveToEndOfText( wxCommandEvent
& WXUNUSED(event
) )
176 { m_panel
->DoMoveToEndOfText(); }
177 void OnMoveToEndOfEntry( wxCommandEvent
& WXUNUSED(event
) )
178 { m_panel
->DoMoveToEndOfEntry(); }
180 void OnScrollLineDown(wxCommandEvent
& WXUNUSED(event
))
182 if ( !m_panel
->m_textrich
->LineDown() )
183 wxLogMessage(_T("Already at the bottom"));
186 void OnScrollLineUp(wxCommandEvent
& WXUNUSED(event
))
188 if ( !m_panel
->m_textrich
->LineUp() )
189 wxLogMessage(_T("Already at the top"));
192 void OnScrollPageDown(wxCommandEvent
& WXUNUSED(event
))
194 if ( !m_panel
->m_textrich
->PageDown() )
195 wxLogMessage(_T("Already at the bottom"));
198 void OnScrollPageUp(wxCommandEvent
& WXUNUSED(event
))
200 if ( !m_panel
->m_textrich
->PageUp() )
201 wxLogMessage(_T("Already at the top"));
205 void OnLogClear(wxCommandEvent
& event
);
207 void OnFileSave(wxCommandEvent
& event
);
208 void OnFileLoad(wxCommandEvent
& event
);
209 void OnRichTextTest(wxCommandEvent
& event
);
211 void OnSetEditable(wxCommandEvent
& event
);
212 void OnSetEnabled(wxCommandEvent
& event
);
214 void OnLogKey(wxCommandEvent
& event
)
216 MyTextCtrl::ms_logKey
= event
.IsChecked();
219 void OnLogChar(wxCommandEvent
& event
)
221 MyTextCtrl::ms_logChar
= event
.IsChecked();
224 void OnLogMouse(wxCommandEvent
& event
)
226 MyTextCtrl::ms_logMouse
= event
.IsChecked();
229 void OnLogText(wxCommandEvent
& event
)
231 MyTextCtrl::ms_logText
= event
.IsChecked();
234 void OnLogFocus(wxCommandEvent
& event
)
236 MyTextCtrl::ms_logFocus
= event
.IsChecked();
239 void OnSetText(wxCommandEvent
& WXUNUSED(event
))
241 m_panel
->m_text
->SetValue(_T("Hello, world (what else did you expect)?"));
244 void OnIdle( wxIdleEvent
& event
);
247 void DoAddText(bool freeze
)
249 wxTextCtrl
* const text
= m_panel
->m_textrich
;
255 for ( int i
= 0; i
< 100; i
++ )
257 text
->AppendText(wxString::Format(wxT("Line %i\n"), i
));
263 text
->SetInsertionPoint(0);
268 DECLARE_EVENT_TABLE()
272 * RichTextFrame is used to demonstrate rich text behaviour
275 class RichTextFrame
: public wxFrame
278 RichTextFrame(wxWindow
* parent
, const wxString
& title
);
282 void OnClose(wxCommandEvent
& event
);
283 void OnIdle(wxIdleEvent
& event
);
284 void OnLeftAlign(wxCommandEvent
& event
);
285 void OnRightAlign(wxCommandEvent
& event
);
286 void OnJustify(wxCommandEvent
& event
);
287 void OnCentre(wxCommandEvent
& event
);
288 void OnChangeFont(wxCommandEvent
& event
);
289 void OnChangeTextColour(wxCommandEvent
& event
);
290 void OnChangeBackgroundColour(wxCommandEvent
& event
);
291 void OnLeftIndent(wxCommandEvent
& event
);
292 void OnRightIndent(wxCommandEvent
& event
);
295 wxTextCtrl
*m_textCtrl
;
296 long m_currentPosition
;
298 DECLARE_EVENT_TABLE()
301 //----------------------------------------------------------------------
303 //----------------------------------------------------------------------
307 //----------------------------------------------------------------------
309 //----------------------------------------------------------------------
321 TEXT_CLIPBOARD_COPY
= 200,
322 TEXT_CLIPBOARD_PASTE
,
325 TEXT_TOOLTIPS_SETDELAY
= 300,
326 TEXT_TOOLTIPS_ENABLE
,
356 // Create the main frame window
357 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
358 _T("Text wxWidgets sample"), 50, 50, 700, 420);
359 frame
->SetSizeHints( 500, 400 );
361 wxMenu
*file_menu
= new wxMenu
;
362 file_menu
->Append(TEXT_SAVE
, _T("&Save file\tCtrl-S"),
363 _T("Save the text control contents to file"));
364 file_menu
->Append(TEXT_LOAD
, _T("&Load file\tCtrl-O"),
365 _T("Load the sample file into text control"));
366 file_menu
->AppendSeparator();
367 file_menu
->Append(TEXT_RICH_TEXT_TEST
, _T("Show Rich Text Editor"));
368 file_menu
->AppendSeparator();
369 file_menu
->Append(TEXT_ABOUT
, _T("&About\tAlt-A"));
370 file_menu
->AppendSeparator();
371 file_menu
->Append(TEXT_QUIT
, _T("E&xit\tAlt-X"), _T("Quit this sample"));
373 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
374 menu_bar
->Append(file_menu
, _T("&File"));
377 wxMenu
*tooltip_menu
= new wxMenu
;
378 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, _T("Set &delay\tCtrl-D"));
379 tooltip_menu
->AppendSeparator();
380 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, _T("&Toggle tooltips\tCtrl-T"),
381 _T("enable/disable tooltips"), true);
382 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, true);
383 menu_bar
->Append(tooltip_menu
, _T("&Tooltips"));
384 #endif // wxUSE_TOOLTIPS
387 wxMenu
*menuClipboard
= new wxMenu
;
388 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, _T("&Copy\tCtrl-C"),
389 _T("Copy the first line to the clipboard"));
390 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, _T("&Paste\tCtrl-V"),
391 _T("Paste from clipboard to the text control"));
392 menu_bar
->Append(menuClipboard
, _T("&Clipboard"));
393 #endif // wxUSE_CLIPBOARD
395 wxMenu
*menuText
= new wxMenu
;
396 menuText
->Append(TEXT_ADD_SOME
, _T("&Append some text\tCtrl-A"));
397 menuText
->Append(TEXT_ADD_FREEZE
, _T("&Append text with freeze/thaw\tShift-Ctrl-A"));
398 menuText
->Append(TEXT_REMOVE
, _T("&Remove first 10 characters\tCtrl-Y"));
399 menuText
->Append(TEXT_REPLACE
, _T("&Replace characters 4 to 8 with ABC\tCtrl-R"));
400 menuText
->Append(TEXT_SELECT
, _T("&Select characters 4 to 8\tCtrl-I"));
401 menuText
->Append(TEXT_SET
, _T("&Set the first text zone value\tCtrl-E"));
402 menuText
->AppendSeparator();
403 menuText
->Append(TEXT_MOVE_ENDTEXT
, _T("Move cursor to the end of &text"));
404 menuText
->Append(TEXT_MOVE_ENDENTRY
, _T("Move cursor to the end of &entry"));
405 menuText
->AppendCheckItem(TEXT_SET_EDITABLE
, _T("Toggle &editable state"));
406 menuText
->AppendCheckItem(TEXT_SET_ENABLED
, _T("Toggle e&nabled state"));
407 menuText
->Check(TEXT_SET_EDITABLE
, true);
408 menuText
->Check(TEXT_SET_ENABLED
, true);
409 menuText
->AppendSeparator();
410 menuText
->Append(TEXT_LINE_DOWN
, _T("Scroll text one line down"));
411 menuText
->Append(TEXT_LINE_UP
, _T("Scroll text one line up"));
412 menuText
->Append(TEXT_PAGE_DOWN
, _T("Scroll text one page down"));
413 menuText
->Append(TEXT_PAGE_DOWN
, _T("Scroll text one page up"));
414 menu_bar
->Append(menuText
, _T("Te&xt"));
417 wxMenu
*menuLog
= new wxMenu
;
418 menuLog
->AppendCheckItem(TEXT_LOG_KEY
, _T("Log &key events"));
419 menuLog
->AppendCheckItem(TEXT_LOG_CHAR
, _T("Log &char events"));
420 menuLog
->AppendCheckItem(TEXT_LOG_MOUSE
, _T("Log &mouse events"));
421 menuLog
->AppendCheckItem(TEXT_LOG_TEXT
, _T("Log &text events"));
422 menuLog
->AppendCheckItem(TEXT_LOG_FOCUS
, _T("Log &focus events"));
423 menuLog
->AppendSeparator();
424 menuLog
->Append(TEXT_CLEAR
, _T("&Clear the log\tCtrl-C"),
425 _T("Clear the log window contents"));
427 // select only the interesting events by default
428 menuLog
->Check(TEXT_LOG_KEY
, true);
429 menuLog
->Check(TEXT_LOG_CHAR
, true);
430 menuLog
->Check(TEXT_LOG_TEXT
, true);
432 MyTextCtrl::ms_logKey
=
433 MyTextCtrl::ms_logChar
=
434 MyTextCtrl::ms_logText
= true;
436 menu_bar
->Append(menuLog
, _T("&Log"));
439 frame
->SetMenuBar(menu_bar
);
449 //----------------------------------------------------------------------
451 //----------------------------------------------------------------------
453 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
454 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
455 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
456 EVT_CHAR(MyTextCtrl::OnChar
)
458 EVT_TEXT(wxID_ANY
, MyTextCtrl::OnText
)
459 EVT_TEXT_URL(wxID_ANY
, MyTextCtrl::OnTextURL
)
460 EVT_TEXT_MAXLEN(wxID_ANY
, MyTextCtrl::OnTextMaxLen
)
462 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
464 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus
)
465 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus
)
468 bool MyTextCtrl::ms_logKey
= false;
469 bool MyTextCtrl::ms_logChar
= false;
470 bool MyTextCtrl::ms_logMouse
= false;
471 bool MyTextCtrl::ms_logText
= false;
472 bool MyTextCtrl::ms_logFocus
= false;
474 void MyTextCtrl::LogKeyEvent(const wxChar
*name
, wxKeyEvent
& event
) const
477 long keycode
= event
.GetKeyCode();
481 case WXK_BACK
: key
= _T("BACK"); break;
482 case WXK_TAB
: key
= _T("TAB"); break;
483 case WXK_RETURN
: key
= _T("RETURN"); break;
484 case WXK_ESCAPE
: key
= _T("ESCAPE"); break;
485 case WXK_SPACE
: key
= _T("SPACE"); break;
486 case WXK_DELETE
: key
= _T("DELETE"); break;
487 case WXK_START
: key
= _T("START"); break;
488 case WXK_LBUTTON
: key
= _T("LBUTTON"); break;
489 case WXK_RBUTTON
: key
= _T("RBUTTON"); break;
490 case WXK_CANCEL
: key
= _T("CANCEL"); break;
491 case WXK_MBUTTON
: key
= _T("MBUTTON"); break;
492 case WXK_CLEAR
: key
= _T("CLEAR"); break;
493 case WXK_SHIFT
: key
= _T("SHIFT"); break;
494 case WXK_ALT
: key
= _T("ALT"); break;
495 case WXK_CONTROL
: key
= _T("CONTROL"); break;
496 case WXK_MENU
: key
= _T("MENU"); break;
497 case WXK_PAUSE
: key
= _T("PAUSE"); break;
498 case WXK_CAPITAL
: key
= _T("CAPITAL"); break;
499 case WXK_PRIOR
: key
= _T("PRIOR"); break;
500 case WXK_NEXT
: key
= _T("NEXT"); break;
501 case WXK_END
: key
= _T("END"); break;
502 case WXK_HOME
: key
= _T("HOME"); break;
503 case WXK_LEFT
: key
= _T("LEFT"); break;
504 case WXK_UP
: key
= _T("UP"); break;
505 case WXK_RIGHT
: key
= _T("RIGHT"); break;
506 case WXK_DOWN
: key
= _T("DOWN"); break;
507 case WXK_SELECT
: key
= _T("SELECT"); break;
508 case WXK_PRINT
: key
= _T("PRINT"); break;
509 case WXK_EXECUTE
: key
= _T("EXECUTE"); break;
510 case WXK_SNAPSHOT
: key
= _T("SNAPSHOT"); break;
511 case WXK_INSERT
: key
= _T("INSERT"); break;
512 case WXK_HELP
: key
= _T("HELP"); break;
513 case WXK_NUMPAD0
: key
= _T("NUMPAD0"); break;
514 case WXK_NUMPAD1
: key
= _T("NUMPAD1"); break;
515 case WXK_NUMPAD2
: key
= _T("NUMPAD2"); break;
516 case WXK_NUMPAD3
: key
= _T("NUMPAD3"); break;
517 case WXK_NUMPAD4
: key
= _T("NUMPAD4"); break;
518 case WXK_NUMPAD5
: key
= _T("NUMPAD5"); break;
519 case WXK_NUMPAD6
: key
= _T("NUMPAD6"); break;
520 case WXK_NUMPAD7
: key
= _T("NUMPAD7"); break;
521 case WXK_NUMPAD8
: key
= _T("NUMPAD8"); break;
522 case WXK_NUMPAD9
: key
= _T("NUMPAD9"); break;
523 case WXK_MULTIPLY
: key
= _T("MULTIPLY"); break;
524 case WXK_ADD
: key
= _T("ADD"); break;
525 case WXK_SEPARATOR
: key
= _T("SEPARATOR"); break;
526 case WXK_SUBTRACT
: key
= _T("SUBTRACT"); break;
527 case WXK_DECIMAL
: key
= _T("DECIMAL"); break;
528 case WXK_DIVIDE
: key
= _T("DIVIDE"); break;
529 case WXK_F1
: key
= _T("F1"); break;
530 case WXK_F2
: key
= _T("F2"); break;
531 case WXK_F3
: key
= _T("F3"); break;
532 case WXK_F4
: key
= _T("F4"); break;
533 case WXK_F5
: key
= _T("F5"); break;
534 case WXK_F6
: key
= _T("F6"); break;
535 case WXK_F7
: key
= _T("F7"); break;
536 case WXK_F8
: key
= _T("F8"); break;
537 case WXK_F9
: key
= _T("F9"); break;
538 case WXK_F10
: key
= _T("F10"); break;
539 case WXK_F11
: key
= _T("F11"); break;
540 case WXK_F12
: key
= _T("F12"); break;
541 case WXK_F13
: key
= _T("F13"); break;
542 case WXK_F14
: key
= _T("F14"); break;
543 case WXK_F15
: key
= _T("F15"); break;
544 case WXK_F16
: key
= _T("F16"); break;
545 case WXK_F17
: key
= _T("F17"); break;
546 case WXK_F18
: key
= _T("F18"); break;
547 case WXK_F19
: key
= _T("F19"); break;
548 case WXK_F20
: key
= _T("F20"); break;
549 case WXK_F21
: key
= _T("F21"); break;
550 case WXK_F22
: key
= _T("F22"); break;
551 case WXK_F23
: key
= _T("F23"); break;
552 case WXK_F24
: key
= _T("F24"); break;
553 case WXK_NUMLOCK
: key
= _T("NUMLOCK"); break;
554 case WXK_SCROLL
: key
= _T("SCROLL"); break;
555 case WXK_PAGEUP
: key
= _T("PAGEUP"); break;
556 case WXK_PAGEDOWN
: key
= _T("PAGEDOWN"); break;
557 case WXK_NUMPAD_SPACE
: key
= _T("NUMPAD_SPACE"); break;
558 case WXK_NUMPAD_TAB
: key
= _T("NUMPAD_TAB"); break;
559 case WXK_NUMPAD_ENTER
: key
= _T("NUMPAD_ENTER"); break;
560 case WXK_NUMPAD_F1
: key
= _T("NUMPAD_F1"); break;
561 case WXK_NUMPAD_F2
: key
= _T("NUMPAD_F2"); break;
562 case WXK_NUMPAD_F3
: key
= _T("NUMPAD_F3"); break;
563 case WXK_NUMPAD_F4
: key
= _T("NUMPAD_F4"); break;
564 case WXK_NUMPAD_HOME
: key
= _T("NUMPAD_HOME"); break;
565 case WXK_NUMPAD_LEFT
: key
= _T("NUMPAD_LEFT"); break;
566 case WXK_NUMPAD_UP
: key
= _T("NUMPAD_UP"); break;
567 case WXK_NUMPAD_RIGHT
: key
= _T("NUMPAD_RIGHT"); break;
568 case WXK_NUMPAD_DOWN
: key
= _T("NUMPAD_DOWN"); break;
569 case WXK_NUMPAD_PRIOR
: key
= _T("NUMPAD_PRIOR"); break;
570 case WXK_NUMPAD_PAGEUP
: key
= _T("NUMPAD_PAGEUP"); break;
571 case WXK_NUMPAD_PAGEDOWN
: key
= _T("NUMPAD_PAGEDOWN"); break;
572 case WXK_NUMPAD_END
: key
= _T("NUMPAD_END"); break;
573 case WXK_NUMPAD_BEGIN
: key
= _T("NUMPAD_BEGIN"); break;
574 case WXK_NUMPAD_INSERT
: key
= _T("NUMPAD_INSERT"); break;
575 case WXK_NUMPAD_DELETE
: key
= _T("NUMPAD_DELETE"); break;
576 case WXK_NUMPAD_EQUAL
: key
= _T("NUMPAD_EQUAL"); break;
577 case WXK_NUMPAD_MULTIPLY
: key
= _T("NUMPAD_MULTIPLY"); break;
578 case WXK_NUMPAD_ADD
: key
= _T("NUMPAD_ADD"); break;
579 case WXK_NUMPAD_SEPARATOR
: key
= _T("NUMPAD_SEPARATOR"); break;
580 case WXK_NUMPAD_SUBTRACT
: key
= _T("NUMPAD_SUBTRACT"); break;
581 case WXK_NUMPAD_DECIMAL
: key
= _T("NUMPAD_DECIMAL"); break;
585 if ( wxIsprint((int)keycode
) )
586 key
.Printf(_T("'%c'"), (char)keycode
);
587 else if ( keycode
> 0 && keycode
< 27 )
588 key
.Printf(_("Ctrl-%c"), _T('A') + keycode
- 1);
590 key
.Printf(_T("unknown (%ld)"), keycode
);
596 key
+= wxString::Format(_T(" (Unicode: %#04x)"), event
.GetUnicodeKey());
597 #endif // wxUSE_UNICODE
599 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
602 GetChar( event
.ControlDown(), _T('C') ),
603 GetChar( event
.AltDown(), _T('A') ),
604 GetChar( event
.ShiftDown(), _T('S') ),
605 GetChar( event
.MetaDown(), _T('M') ) );
608 static wxString
GetMouseEventDesc(const wxMouseEvent
& ev
)
613 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
616 dbl
= ev
.LeftDClick();
619 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
621 button
= _T("Middle");
622 dbl
= ev
.MiddleDClick();
625 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
627 button
= _T("Right");
628 dbl
= ev
.RightDClick();
633 return _T("Unknown mouse event");
636 return wxString::Format(_T("%s mouse button %s"),
638 dbl
? _T("double clicked")
639 : up
? _T("released") : _T("clicked"));
642 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
654 msg
= _T("Mouse entered the window");
656 else if ( ev
.Leaving() )
658 msg
= _T("Mouse left the window");
662 msg
= GetMouseEventDesc(ev
);
665 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ")
667 << GetChar( ev
.LeftIsDown(), _T('1') )
668 << GetChar( ev
.MiddleIsDown(), _T('2') )
669 << GetChar( ev
.RightIsDown(), _T('3') )
670 << GetChar( ev
.ControlDown(), _T('C') )
671 << GetChar( ev
.AltDown(), _T('A') )
672 << GetChar( ev
.ShiftDown(), _T('S') )
673 << GetChar( ev
.MetaDown(), _T('M') );
677 //else: we're not interested in mouse move events
680 void MyTextCtrl::OnSetFocus(wxFocusEvent
& event
)
683 wxLogMessage( wxT("%p got focus."), this);
688 void MyTextCtrl::OnKillFocus(wxFocusEvent
& event
)
691 wxLogMessage( wxT("%p lost focus"), this);
696 void MyTextCtrl::OnText(wxCommandEvent
& event
)
701 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
702 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
705 wxLogMessage(_T("Text changed in control '%s'"), data
);
709 // wxLogMessage( event.GetString() );
710 wxLogMessage(_T("Text changed in some control"));
714 void MyTextCtrl::OnTextMaxLen(wxCommandEvent
& WXUNUSED(event
))
716 wxLogMessage(_T("You can't enter more characters into this control."));
719 void MyTextCtrl::OnTextURL(wxTextUrlEvent
& event
)
721 const wxMouseEvent
& ev
= event
.GetMouseEvent();
723 // filter out mouse moves, too many of them
727 long start
= event
.GetURLStart(),
728 end
= event
.GetURLEnd();
730 wxLogMessage(_T("Mouse event over URL '%s': %s"),
731 GetValue().Mid(start
, end
- start
).c_str(),
732 GetMouseEventDesc(ev
).c_str());
735 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
738 LogKeyEvent( _T("Char"), event
);
743 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
746 LogKeyEvent( _T("Key up"), event
);
751 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
753 switch ( event
.GetKeyCode() )
756 // show current position and text length
758 long line
, column
, pos
= GetInsertionPoint();
759 PositionToXY(pos
, &column
, &line
);
761 wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
764 (long) GetNumberOfLines(),
765 (long) GetLineLength(line
),
770 GetSelection(&from
, &to
);
772 wxString sel
= GetStringSelection();
774 wxLogMessage(_T("Selection: from %ld to %ld."), from
, to
);
775 wxLogMessage(_T("Selection = '%s' (len = %u)"),
776 sel
.c_str(), sel
.length());
782 SetInsertionPointEnd();
787 SetInsertionPoint(10);
793 wxLogDebug( wxT("Now capturing mouse and events.") );
799 wxLogDebug( wxT("Stopped capturing mouse and events.") );
800 m_hasCapture
= false;
806 // insert a blank line
811 wxLogMessage(_T("IsModified() before SetValue(): %d"),
813 SetValue(_T("SetValue() has been called"));
814 wxLogMessage(_T("IsModified() after SetValue(): %d"),
819 wxLogMessage(_T("Position 10 should be now visible."));
824 wxLogMessage(_T("Control has been cleared"));
829 WriteText(_T("WriteText() has been called"));
833 AppendText(_T("AppendText() has been called"));
838 LogKeyEvent( wxT("Key down"), event
);
843 //----------------------------------------------------------------------
845 //----------------------------------------------------------------------
847 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
848 EVT_SIZE(MyPanel::OnSize
)
851 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
852 : wxPanel( frame
, wxID_ANY
, wxPoint(x
, y
), wxSize(w
, h
) )
855 m_log
= new wxTextCtrl( this, wxID_ANY
, _T("This is the log window.\n"),
856 wxPoint(5,260), wxSize(630,100),
857 wxTE_MULTILINE
| wxTE_READONLY
/* | wxTE_RICH */);
859 m_logOld
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
862 // single line text controls
864 m_text
= new MyTextCtrl( this, wxID_ANY
, _T("Single line."),
865 wxDefaultPosition
, wxDefaultSize
,
867 m_text
->SetForegroundColour(*wxBLUE
);
868 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
869 (*m_text
) << _T(" Appended.");
870 m_text
->SetInsertionPoint(0);
871 m_text
->WriteText( _T("Prepended. ") );
873 m_password
= new MyTextCtrl( this, wxID_ANY
, _T(""),
874 wxPoint(10,50), wxSize(140,wxDefaultCoord
), wxTE_PASSWORD
);
876 m_readonly
= new MyTextCtrl( this, wxID_ANY
, _T("Read only"),
877 wxPoint(10,90), wxSize(140,wxDefaultCoord
), wxTE_READONLY
);
879 m_limited
= new MyTextCtrl(this, wxID_ANY
, _T("Max 8 ch"),
880 wxPoint(10, 130), wxSize(140, wxDefaultCoord
));
881 m_limited
->SetMaxLength(8);
883 // multi line text controls
885 m_horizontal
= new MyTextCtrl( this, wxID_ANY
, _T("Multiline text control with a horizontal scrollbar.\n"),
886 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
888 // a little hack to use the command line argument for encoding testing
889 if ( wxTheApp
->argc
== 2 )
891 switch ( wxTheApp
->argv
[1][0] )
894 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
896 wxFONTENCODING_ISO8859_2
));
897 m_horizontal
->AppendText(_T("®lu»ouèký kùò zbìsile èe¹tina «»"));
901 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
903 wxFONTENCODING_CP1251
));
904 m_horizontal
->AppendText(_T("Ïðèâåò!"));
908 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
910 wxFONTENCODING_CP1251
));
912 m_horizontal
->AppendText(L
"\x0412\x0430\x0434\x0438\x043c \x0426");
914 m_horizontal
->AppendText("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
920 m_horizontal
->AppendText(_T("Text in default encoding"));
923 m_multitext
= new MyTextCtrl( this, wxID_ANY
, _T("Multi line."),
924 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
925 m_multitext
->SetFont(*wxITALIC_FONT
);
926 (*m_multitext
) << _T(" Appended.");
927 m_multitext
->SetInsertionPoint(0);
928 m_multitext
->WriteText( _T("Prepended. ") );
929 m_multitext
->SetForegroundColour(*wxRED
);
930 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
933 m_multitext
->SetToolTip(_T("Press Fn function keys here"));
936 m_tab
= new MyTextCtrl( this, 100, _T("Multiline, allow <TAB> processing."),
937 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
938 m_tab
->SetClientData((void *)_T("tab"));
940 m_enter
= new MyTextCtrl( this, 100, _T("Multiline, allow <ENTER> processing."),
941 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
942 m_enter
->SetClientData((void *)_T("enter"));
944 m_textrich
= new MyTextCtrl(this, wxID_ANY
, _T("Allows more than 30Kb of text\n")
945 _T("(even under broken Win9x)\n")
946 _T("and a very very very very very ")
947 _T("very very very long line to test ")
948 _T("wxHSCROLL style"),
949 wxPoint(450, 10), wxSize(230, 230),
950 wxTE_RICH
| wxTE_MULTILINE
);
951 m_textrich
->SetStyle(0, 10, *wxRED
);
952 m_textrich
->SetStyle(10, 20, *wxBLUE
);
953 m_textrich
->SetStyle(30, 40,
954 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
955 m_textrich
->SetDefaultStyle(wxTextAttr());
956 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
957 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
958 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
959 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
960 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
961 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
962 m_textrich
->SetDefaultStyle(wxTextAttr(*wxBLUE
, *wxWHITE
));
963 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
964 _T("type should be in blue as well"));
966 wxBoxSizer
*column1
= new wxBoxSizer(wxVERTICAL
);
967 column1
->Add( m_text
, 0, wxALL
, 10 );
968 column1
->Add( m_password
, 0, wxALL
, 10 );
969 column1
->Add( m_readonly
, 0, wxALL
, 10 );
970 column1
->Add( m_limited
, 0, wxALL
, 10 );
971 column1
->Add( m_horizontal
, 1, wxALL
| wxEXPAND
, 10 );
973 wxBoxSizer
*column2
= new wxBoxSizer(wxVERTICAL
);
974 column2
->Add( m_multitext
, 1, wxALL
| wxEXPAND
, 10 );
975 column2
->Add( m_tab
, 1, wxALL
| wxEXPAND
, 10 );
976 column2
->Add( m_enter
, 1, wxALL
| wxEXPAND
, 10 );
978 wxBoxSizer
*column3
= new wxBoxSizer(wxVERTICAL
);
979 column3
->Add( m_textrich
, 1, wxALL
| wxEXPAND
, 10 );
981 wxBoxSizer
*row1
= new wxBoxSizer(wxHORIZONTAL
);
982 row1
->Add( column1
, 0, wxALL
| wxEXPAND
, 10 );
983 row1
->Add( column2
, 1, wxALL
| wxEXPAND
, 10 );
984 row1
->Add( column3
, 1, wxALL
| wxEXPAND
, 10 );
986 wxBoxSizer
*topSizer
= new wxBoxSizer(wxVERTICAL
);
987 topSizer
->Add( row1
, 2, wxALL
| wxEXPAND
, 10 );
990 wxBoxSizer
*row2
= new wxBoxSizer(wxHORIZONTAL
);
991 row2
->Add( m_log
, 1, wxALL
| wxEXPAND
, 10 );
992 topSizer
->Add( row2
, 1, wxALL
| wxEXPAND
, 10 );
995 SetAutoLayout( true );
999 void MyPanel::OnSize( wxSizeEvent
&event
)
1002 wxSize
client_area( GetClientSize() );
1004 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
1009 wxTextCtrl
*MyPanel::GetFocusedText(wxTextCtrl
*textDef
)
1011 wxWindow
*win
= FindFocus();
1013 wxTextCtrl
*text
= win
? wxDynamicCast(win
, wxTextCtrl
) : NULL
;
1014 return text
? text
: textDef
;
1018 void MyPanel::DoPasteFromClipboard()
1020 // On X11, we want to get the data from the primary selection instead
1021 // of the normal clipboard (which isn't normal under X11 at all). This
1022 // call has no effect under MSW.
1023 wxTheClipboard
->UsePrimarySelection();
1025 if (!wxTheClipboard
->Open())
1028 *m_log
<< _T("Error opening the clipboard.\n");
1035 *m_log
<< _T("Successfully opened the clipboard.\n");
1039 wxTextDataObject data
;
1041 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
1044 *m_log
<< _T("Clipboard supports requested format.\n");
1047 if (wxTheClipboard
->GetData( data
))
1050 *m_log
<< _T("Successfully retrieved data from the clipboard.\n");
1052 *m_multitext
<< data
.GetText() << _T("\n");
1057 *m_log
<< _T("Error getting data from the clipboard.\n");
1064 *m_log
<< _T("Clipboard doesn't support requested format.\n");
1068 wxTheClipboard
->Close();
1071 *m_log
<< _T("Closed the clipboard.\n");
1075 void MyPanel::DoCopyToClipboard()
1077 // On X11, we want to get the data from the primary selection instead
1078 // of the normal clipboard (which isn't normal under X11 at all). This
1079 // call has no effect under MSW.
1080 wxTheClipboard
->UsePrimarySelection();
1082 wxString
text( m_multitext
->GetLineText(0) );
1087 *m_log
<< _T("No text to copy.\n");
1093 if (!wxTheClipboard
->Open())
1096 *m_log
<< _T("Error opening the clipboard.\n");
1104 *m_log
<< _T("Successfully opened the clipboard.\n");
1108 wxTextDataObject
*data
= new wxTextDataObject( text
);
1110 if (!wxTheClipboard
->SetData( data
))
1113 *m_log
<< _T("Error while copying to the clipboard.\n");
1119 *m_log
<< _T("Successfully copied data to the clipboard.\n");
1123 wxTheClipboard
->Close();
1126 *m_log
<< _T("Closed the clipboard.\n");
1130 #endif // wxUSE_CLIPBOARD
1132 void MyPanel::DoMoveToEndOfText()
1134 m_multitext
->SetInsertionPointEnd();
1135 m_multitext
->SetFocus();
1138 void MyPanel::DoMoveToEndOfEntry()
1140 m_text
->SetInsertionPointEnd();
1144 void MyPanel::DoRemoveText()
1146 GetFocusedText(m_multitext
)->Remove(0, 10);
1149 void MyPanel::DoReplaceText()
1151 GetFocusedText(m_multitext
)->Replace(3, 8, _T("ABC"));
1154 void MyPanel::DoSelectText()
1156 GetFocusedText(m_multitext
)->SetSelection(3, 8);
1159 //----------------------------------------------------------------------
1161 //----------------------------------------------------------------------
1163 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1164 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
1165 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
1166 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
1167 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
1168 EVT_MENU(TEXT_RICH_TEXT_TEST
, MyFrame::OnRichTextTest
)
1170 EVT_MENU(TEXT_LOG_KEY
, MyFrame::OnLogKey
)
1171 EVT_MENU(TEXT_LOG_CHAR
, MyFrame::OnLogChar
)
1172 EVT_MENU(TEXT_LOG_MOUSE
,MyFrame::OnLogMouse
)
1173 EVT_MENU(TEXT_LOG_TEXT
, MyFrame::OnLogText
)
1174 EVT_MENU(TEXT_LOG_FOCUS
,MyFrame::OnLogFocus
)
1176 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
1180 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
1181 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
1182 #endif // wxUSE_TOOLTIPS
1185 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
1186 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
1187 #endif // wxUSE_CLIPBOARD
1189 EVT_MENU(TEXT_REMOVE
, MyFrame::OnRemoveText
)
1190 EVT_MENU(TEXT_REPLACE
, MyFrame::OnReplaceText
)
1191 EVT_MENU(TEXT_SELECT
, MyFrame::OnSelectText
)
1192 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
1193 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
1194 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
1195 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
1197 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
1198 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
1200 EVT_MENU(TEXT_LINE_DOWN
, MyFrame::OnScrollLineDown
)
1201 EVT_MENU(TEXT_LINE_UP
, MyFrame::OnScrollLineUp
)
1202 EVT_MENU(TEXT_PAGE_DOWN
, MyFrame::OnScrollPageDown
)
1203 EVT_MENU(TEXT_PAGE_UP
, MyFrame::OnScrollPageUp
)
1205 EVT_MENU(TEXT_SET
, MyFrame::OnSetText
)
1207 EVT_IDLE(MyFrame::OnIdle
)
1210 MyFrame::MyFrame(wxFrame
*frame
, const wxChar
*title
, int x
, int y
, int w
, int h
)
1211 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
) )
1215 #endif // wxUSE_STATUSBAR
1217 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
1220 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
1225 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
1227 wxBeginBusyCursor();
1229 wxMessageDialog
dialog(this,
1230 _T("This is a text control sample. It demonstrates the many different\n")
1231 _T("text control styles, the use of the clipboard, setting and handling\n")
1232 _T("tooltips and intercepting key and char events.\n")
1234 _T("Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin"),
1235 _T("About wxTextCtrl Sample"),
1236 wxOK
| wxICON_INFORMATION
);
1245 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& WXUNUSED(event
))
1247 static long s_delay
= 5000;
1250 delay
.Printf( _T("%ld"), s_delay
);
1252 delay
= wxGetTextFromUser(_T("Enter delay (in milliseconds)"),
1253 _T("Set tooltip delay"),
1257 return; // cancelled
1259 wxSscanf(delay
, _T("%ld"), &s_delay
);
1261 wxToolTip::SetDelay(s_delay
);
1263 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
1266 void MyFrame::OnToggleTooltips(wxCommandEvent
& WXUNUSED(event
))
1268 static bool s_enabled
= true;
1270 s_enabled
= !s_enabled
;
1272 wxToolTip::Enable(s_enabled
);
1274 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
1279 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
1281 m_panel
->m_log
->Clear();
1285 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
1287 static bool s_editable
= true;
1289 s_editable
= !s_editable
;
1290 m_panel
->m_text
->SetEditable(s_editable
);
1291 m_panel
->m_password
->SetEditable(s_editable
);
1292 m_panel
->m_multitext
->SetEditable(s_editable
);
1293 m_panel
->m_textrich
->SetEditable(s_editable
);
1296 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
1298 bool enabled
= m_panel
->m_text
->IsEnabled();
1301 m_panel
->m_text
->Enable(enabled
);
1302 m_panel
->m_password
->Enable(enabled
);
1303 m_panel
->m_multitext
->Enable(enabled
);
1304 m_panel
->m_readonly
->Enable(enabled
);
1305 m_panel
->m_limited
->Enable(enabled
);
1306 m_panel
->m_textrich
->Enable(enabled
);
1309 void MyFrame::OnFileSave(wxCommandEvent
& WXUNUSED(event
))
1311 if ( m_panel
->m_textrich
->SaveFile(_T("dummy.txt")) )
1314 // verify that the fil length is correct (it wasn't under Win95)
1315 wxFile
file(wxT("dummy.txt"));
1317 _T("Successfully saved file (text len = %lu, file size = %ld)"),
1318 (unsigned long)m_panel
->m_textrich
->GetValue().length(),
1319 (long) file
.Length());
1323 wxLogStatus(this, _T("Couldn't save the file"));
1326 void MyFrame::OnFileLoad(wxCommandEvent
& WXUNUSED(event
))
1328 if ( m_panel
->m_textrich
->LoadFile(_T("dummy.txt")) )
1329 wxLogStatus(this, _T("Successfully loaded file"));
1331 wxLogStatus(this, _T("Couldn't load the file"));
1334 void MyFrame::OnRichTextTest(wxCommandEvent
& WXUNUSED(event
))
1336 RichTextFrame
* frame
= new RichTextFrame(this, _T("Rich Text Editor"));
1340 void MyFrame::OnIdle( wxIdleEvent
& event
)
1342 // track the window which has the focus in the status bar
1343 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
1344 wxWindow
*focus
= wxWindow::FindFocus();
1345 if ( focus
&& (focus
!= s_windowFocus
) )
1347 s_windowFocus
= focus
;
1352 _T("Focus: wxWindow = %p, HWND = %08x"),
1354 _T("Focus: wxWindow = %p"),
1358 , (unsigned int) s_windowFocus
->GetHWND()
1364 #endif // wxUSE_STATUSBAR
1370 * RichTextFrame is used to demonstrate rich text behaviour
1375 RICHTEXT_CLOSE
= 1000,
1376 RICHTEXT_LEFT_ALIGN
,
1377 RICHTEXT_RIGHT_ALIGN
,
1380 RICHTEXT_CHANGE_FONT
,
1381 RICHTEXT_CHANGE_TEXT_COLOUR
,
1382 RICHTEXT_CHANGE_BACKGROUND_COLOUR
,
1383 RICHTEXT_LEFT_INDENT
,
1384 RICHTEXT_RIGHT_INDENT
1387 BEGIN_EVENT_TABLE(RichTextFrame
, wxFrame
)
1388 EVT_IDLE(RichTextFrame::OnIdle
)
1389 EVT_MENU(RICHTEXT_CLOSE
, RichTextFrame::OnClose
)
1390 EVT_MENU(RICHTEXT_LEFT_ALIGN
, RichTextFrame::OnLeftAlign
)
1391 EVT_MENU(RICHTEXT_RIGHT_ALIGN
, RichTextFrame::OnRightAlign
)
1392 EVT_MENU(RICHTEXT_CENTRE
, RichTextFrame::OnCentre
)
1393 EVT_MENU(RICHTEXT_JUSTIFY
, RichTextFrame::OnJustify
)
1394 EVT_MENU(RICHTEXT_CHANGE_FONT
, RichTextFrame::OnChangeFont
)
1395 EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR
, RichTextFrame::OnChangeTextColour
)
1396 EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, RichTextFrame::OnChangeBackgroundColour
)
1397 EVT_MENU(RICHTEXT_LEFT_INDENT
, RichTextFrame::OnLeftIndent
)
1398 EVT_MENU(RICHTEXT_RIGHT_INDENT
, RichTextFrame::OnRightIndent
)
1401 RichTextFrame::RichTextFrame(wxWindow
* parent
, const wxString
& title
):
1402 wxFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxSize(300, 400))
1404 m_currentPosition
= -1;
1405 m_textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1406 wxDefaultSize
, wxTE_MULTILINE
|wxTE_RICH2
);
1410 for (i
= 0; i
< 10; i
++)
1413 for (j
= 0; j
< 10; j
++)
1415 value
<< wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
1417 value
<< wxT("\n\n");
1419 m_textCtrl
->SetValue(value
);
1421 wxMenuBar
* menuBar
= new wxMenuBar
;
1422 wxMenu
* fileMenu
= new wxMenu
;
1423 fileMenu
->Append(RICHTEXT_CLOSE
, _("Close\tCtrl+W"));
1424 menuBar
->Append(fileMenu
, _("File"));
1426 wxMenu
* editMenu
= new wxMenu
;
1427 editMenu
->Append(RICHTEXT_LEFT_ALIGN
, _("Left Align"));
1428 editMenu
->Append(RICHTEXT_RIGHT_ALIGN
, _("Right Align"));
1429 editMenu
->Append(RICHTEXT_CENTRE
, _("Centre"));
1430 editMenu
->Append(RICHTEXT_JUSTIFY
, _("Justify"));
1431 editMenu
->AppendSeparator();
1432 editMenu
->Append(RICHTEXT_CHANGE_FONT
, _("Change Font"));
1433 editMenu
->Append(RICHTEXT_CHANGE_TEXT_COLOUR
, _("Change Text Colour"));
1434 editMenu
->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR
, _("Change Background Colour"));
1435 editMenu
->AppendSeparator();
1436 editMenu
->Append(RICHTEXT_LEFT_INDENT
, _("Left Indent"));
1437 editMenu
->Append(RICHTEXT_RIGHT_INDENT
, _("Right indent"));
1438 menuBar
->Append(editMenu
, _("Edit"));
1440 SetMenuBar(menuBar
);
1443 #endif // wxUSE_STATUSBAR
1448 void RichTextFrame::OnClose(wxCommandEvent
& WXUNUSED(event
))
1453 void RichTextFrame::OnLeftAlign(wxCommandEvent
& WXUNUSED(event
))
1456 attr
.SetAlignment(wxTEXT_ALIGNMENT_LEFT
);
1459 m_textCtrl
->GetSelection(& start
, & end
);
1460 m_textCtrl
->SetStyle(start
, end
, attr
);
1462 m_currentPosition
= -1;
1465 void RichTextFrame::OnRightAlign(wxCommandEvent
& WXUNUSED(event
))
1468 attr
.SetAlignment(wxTEXT_ALIGNMENT_RIGHT
);
1471 m_textCtrl
->GetSelection(& start
, & end
);
1472 m_textCtrl
->SetStyle(start
, end
, attr
);
1474 m_currentPosition
= -1;
1477 void RichTextFrame::OnJustify(wxCommandEvent
& WXUNUSED(event
))
1480 attr
.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED
);
1483 m_textCtrl
->GetSelection(& start
, & end
);
1484 m_textCtrl
->SetStyle(start
, end
, attr
);
1486 m_currentPosition
= -1;
1489 void RichTextFrame::OnCentre(wxCommandEvent
& WXUNUSED(event
))
1492 attr
.SetAlignment(wxTEXT_ALIGNMENT_CENTRE
);
1495 m_textCtrl
->GetSelection(& start
, & end
);
1496 m_textCtrl
->SetStyle(start
, end
, attr
);
1498 m_currentPosition
= -1;
1501 void RichTextFrame::OnChangeFont(wxCommandEvent
& WXUNUSED(event
))
1505 wxFontDialog
dialog(this, data
);
1507 if (dialog
.ShowModal() == wxID_OK
)
1509 wxFontData retData
= dialog
.GetFontData();
1510 wxFont font
= retData
.GetChosenFont();
1516 m_textCtrl
->GetSelection(& start
, & end
);
1517 m_textCtrl
->SetStyle(start
, end
, attr
);
1519 m_currentPosition
= -1;
1523 void RichTextFrame::OnChangeTextColour(wxCommandEvent
& WXUNUSED(event
))
1526 data
.SetColour(* wxBLACK
);
1527 data
.SetChooseFull(true);
1528 for (int i
= 0; i
< 16; i
++)
1530 wxColour
colour(i
*16, i
*16, i
*16);
1531 data
.SetCustomColour(i
, colour
);
1534 wxColourDialog
dialog(this, &data
);
1535 dialog
.SetTitle(_T("Choose the text colour"));
1536 if (dialog
.ShowModal() == wxID_OK
)
1538 wxColourData retData
= dialog
.GetColourData();
1539 wxColour col
= retData
.GetColour();
1542 attr
.SetTextColour(col
);
1545 m_textCtrl
->GetSelection(& start
, & end
);
1546 m_textCtrl
->SetStyle(start
, end
, attr
);
1548 m_currentPosition
= -1;
1552 void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent
& WXUNUSED(event
))
1555 data
.SetColour(* wxWHITE
);
1556 data
.SetChooseFull(true);
1557 for (int i
= 0; i
< 16; i
++)
1559 wxColour
colour(i
*16, i
*16, i
*16);
1560 data
.SetCustomColour(i
, colour
);
1563 wxColourDialog
dialog(this, &data
);
1564 dialog
.SetTitle(_T("Choose the text background colour"));
1565 if (dialog
.ShowModal() == wxID_OK
)
1567 wxColourData retData
= dialog
.GetColourData();
1568 wxColour col
= retData
.GetColour();
1571 attr
.SetBackgroundColour(col
);
1574 m_textCtrl
->GetSelection(& start
, & end
);
1575 m_textCtrl
->SetStyle(start
, end
, attr
);
1577 m_currentPosition
= -1;
1581 void RichTextFrame::OnLeftIndent(wxCommandEvent
& WXUNUSED(event
))
1583 wxString indentStr
= wxGetTextFromUser
1585 _("Please enter the left indent in tenths of a millimetre."),
1590 if (!indentStr
.IsEmpty())
1592 int indent
= wxAtoi(indentStr
);
1595 attr
.SetLeftIndent(indent
);
1598 m_textCtrl
->GetSelection(& start
, & end
);
1599 m_textCtrl
->SetStyle(start
, end
, attr
);
1601 m_currentPosition
= -1;
1605 void RichTextFrame::OnRightIndent(wxCommandEvent
& WXUNUSED(event
))
1607 wxString indentStr
= wxGetTextFromUser
1609 _("Please enter the right indent in tenths of a millimetre."),
1614 if (!indentStr
.IsEmpty())
1616 int indent
= wxAtoi(indentStr
);
1619 attr
.SetRightIndent(indent
);
1622 m_textCtrl
->GetSelection(& start
, & end
);
1623 m_textCtrl
->SetStyle(start
, end
, attr
);
1625 m_currentPosition
= -1;
1629 void RichTextFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
1631 long insertionPoint
= m_textCtrl
->GetInsertionPoint();
1632 if (insertionPoint
!= m_currentPosition
)
1636 if (m_textCtrl
->GetStyle(insertionPoint
, attr
))
1639 wxString
facename(wxT("unknown"));
1640 if (attr
.GetFont().Ok())
1642 facename
= attr
.GetFont().GetFaceName();
1644 wxString
alignment(wxT("unknown alignment"));
1645 if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE
)
1646 alignment
= wxT("centred");
1647 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT
)
1648 alignment
= wxT("right-aligned");
1649 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_LEFT
)
1650 alignment
= wxT("left-aligned");
1651 else if (attr
.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED
)
1652 alignment
= wxT("justified");
1653 msg
.Printf(wxT("Facename: %s, wxColour(%d, %d, %d), %s"),
1654 (const wxChar
*) facename
,
1655 attr
.GetTextColour().Red(), attr
.GetTextColour().Green(), attr
.GetTextColour().Blue(),
1656 (const wxChar
*) alignment
);
1659 #endif // wxUSE_STATUSBAR
1660 m_currentPosition
= insertionPoint
;