]>
git.saurik.com Git - wxWidgets.git/blob - samples/text/text.cpp
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 //----------------------------------------------------------------------
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
) { m_hasCapture
= FALSE
; }
66 void OnKeyDown(wxKeyEvent
& event
);
67 void OnKeyUp(wxKeyEvent
& event
);
68 void OnChar(wxKeyEvent
& event
);
69 void OnText(wxCommandEvent
& event
);
70 void OnMouseEvent(wxMouseEvent
& event
);
75 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
76 void LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
81 class MyPanel
: public wxPanel
84 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
87 void DoPasteFromClipboard();
88 void DoCopyToClipboard();
89 #endif // wxUSE_CLIPBOARD
91 void DoMoveToEndOfText();
92 void DoMoveToEndOfEntry();
94 void OnSize( wxSizeEvent
&event
);
97 MyTextCtrl
*m_password
;
100 MyTextCtrl
*m_readonly
;
102 MyTextCtrl
*m_multitext
;
103 MyTextCtrl
*m_horizontal
;
105 MyTextCtrl
*m_textrich
;
110 DECLARE_EVENT_TABLE()
113 class MyFrame
: public wxFrame
116 MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
);
118 void OnQuit(wxCommandEvent
& event
);
119 void OnAbout(wxCommandEvent
& event
);
121 void OnSetTooltipDelay(wxCommandEvent
& event
);
122 void OnToggleTooltips(wxCommandEvent
& event
);
123 #endif // wxUSE_TOOLTIPS
126 void OnPasteFromClipboard( wxCommandEvent
&event
)
127 { m_panel
->DoPasteFromClipboard(); }
128 void OnCopyToClipboard( wxCommandEvent
&event
)
129 { m_panel
->DoCopyToClipboard(); }
130 #endif // wxUSE_CLIPBOARD
132 void OnAddTextFreeze( wxCommandEvent
& event
)
134 void OnAddText( wxCommandEvent
& event
)
135 { DoAddText(false); }
137 void OnMoveToEndOfText( wxCommandEvent
&event
)
138 { m_panel
->DoMoveToEndOfText(); }
139 void OnMoveToEndOfEntry( wxCommandEvent
&event
)
140 { m_panel
->DoMoveToEndOfEntry(); }
142 void OnLogClear(wxCommandEvent
& event
);
143 void OnFileSave(wxCommandEvent
& event
);
144 void OnFileLoad(wxCommandEvent
& event
);
146 void OnSetEditable(wxCommandEvent
& event
);
147 void OnSetEnabled(wxCommandEvent
& event
);
149 void OnIdle( wxIdleEvent
& event
);
152 void DoAddText(bool freeze
)
154 wxTextCtrl
*text
= m_panel
->m_textrich
;
158 for ( int i
= 0; i
< 100; i
++ )
159 text
->AppendText(wxString::Format("Line %i\n", i
));
161 text
->SetInsertionPoint(0);
169 DECLARE_EVENT_TABLE()
172 //----------------------------------------------------------------------
174 //----------------------------------------------------------------------
178 //----------------------------------------------------------------------
180 //----------------------------------------------------------------------
191 TEXT_CLIPBOARD_COPY
= 200,
192 TEXT_CLIPBOARD_PASTE
,
195 TEXT_TOOLTIPS_SETDELAY
= 300,
196 TEXT_TOOLTIPS_ENABLE
,
209 // Create the main frame window
210 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
211 "Text wxWindows sample", 50, 50, 700, 420);
212 frame
->SetSizeHints( 500, 400 );
214 wxMenu
*file_menu
= new wxMenu
;
215 file_menu
->Append(TEXT_CLEAR
, "&Clear the log\tCtrl-C",
216 "Clear the log window contents");
217 file_menu
->Append(TEXT_SAVE
, "&Save file\tCtrl-S",
218 "Save the text control contents to file");
219 file_menu
->Append(TEXT_LOAD
, "&Load file\tCtrl-O",
220 "Load the sample file into text control");
221 file_menu
->AppendSeparator();
222 file_menu
->Append(TEXT_ABOUT
, "&About\tAlt-A");
223 file_menu
->AppendSeparator();
224 file_menu
->Append(TEXT_QUIT
, "E&xit\tAlt-X", "Quit this sample");
226 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
227 menu_bar
->Append(file_menu
, "&File");
230 wxMenu
*tooltip_menu
= new wxMenu
;
231 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, "Set &delay\tCtrl-D");
232 tooltip_menu
->AppendSeparator();
233 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, "&Toggle tooltips\tCtrl-T",
234 "enable/disable tooltips", TRUE
);
235 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, TRUE
);
236 menu_bar
->Append(tooltip_menu
, "&Tooltips");
237 #endif // wxUSE_TOOLTIPS
240 wxMenu
*menuClipboard
= new wxMenu
;
241 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, "&Copy\tCtrl-C",
242 "Copy the first line to the clipboard");
243 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, "&Paste\tCtrl-V",
244 "Paste from clipboard to the text control");
245 menu_bar
->Append(menuClipboard
, "&Clipboard");
246 #endif // wxUSE_CLIPBOARD
248 wxMenu
*menuText
= new wxMenu
;
249 menuText
->Append(TEXT_ADD_SOME
, "&Append some text\tCtrl-A");
250 menuText
->Append(TEXT_ADD_FREEZE
, "&Append text with freeze/thaw\tShift-Ctrl-A");
251 menuText
->Append(TEXT_MOVE_ENDTEXT
, "Move cursor to the end of &text");
252 menuText
->Append(TEXT_MOVE_ENDENTRY
, "Move cursor to the end of &entry");
253 menuText
->Append(TEXT_SET_EDITABLE
, "Toggle &editable state", "", TRUE
);
254 menuText
->Append(TEXT_SET_ENABLED
, "Toggle e&nabled state", "", TRUE
);
255 menuText
->Check(TEXT_SET_EDITABLE
, TRUE
);
256 menuText
->Check(TEXT_SET_ENABLED
, TRUE
);
257 menu_bar
->Append(menuText
, "&Text");
259 frame
->SetMenuBar(menu_bar
);
269 //----------------------------------------------------------------------
271 //----------------------------------------------------------------------
273 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
274 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
275 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
276 EVT_CHAR(MyTextCtrl::OnChar
)
277 EVT_TEXT(-1, MyTextCtrl::OnText
)
278 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
281 void MyTextCtrl::LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const
284 long keycode
= event
.KeyCode();
288 case WXK_BACK
: key
= "BACK"; break;
289 case WXK_TAB
: key
= "TAB"; break;
290 case WXK_RETURN
: key
= "RETURN"; break;
291 case WXK_ESCAPE
: key
= "ESCAPE"; break;
292 case WXK_SPACE
: key
= "SPACE"; break;
293 case WXK_DELETE
: key
= "DELETE"; break;
294 case WXK_START
: key
= "START"; break;
295 case WXK_LBUTTON
: key
= "LBUTTON"; break;
296 case WXK_RBUTTON
: key
= "RBUTTON"; break;
297 case WXK_CANCEL
: key
= "CANCEL"; break;
298 case WXK_MBUTTON
: key
= "MBUTTON"; break;
299 case WXK_CLEAR
: key
= "CLEAR"; break;
300 case WXK_SHIFT
: key
= "SHIFT"; break;
301 case WXK_ALT
: key
= "ALT"; break;
302 case WXK_CONTROL
: key
= "CONTROL"; break;
303 case WXK_MENU
: key
= "MENU"; break;
304 case WXK_PAUSE
: key
= "PAUSE"; break;
305 case WXK_CAPITAL
: key
= "CAPITAL"; break;
306 case WXK_PRIOR
: key
= "PRIOR"; break;
307 case WXK_NEXT
: key
= "NEXT"; break;
308 case WXK_END
: key
= "END"; break;
309 case WXK_HOME
: key
= "HOME"; break;
310 case WXK_LEFT
: key
= "LEFT"; break;
311 case WXK_UP
: key
= "UP"; break;
312 case WXK_RIGHT
: key
= "RIGHT"; break;
313 case WXK_DOWN
: key
= "DOWN"; break;
314 case WXK_SELECT
: key
= "SELECT"; break;
315 case WXK_PRINT
: key
= "PRINT"; break;
316 case WXK_EXECUTE
: key
= "EXECUTE"; break;
317 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
318 case WXK_INSERT
: key
= "INSERT"; break;
319 case WXK_HELP
: key
= "HELP"; break;
320 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
321 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
322 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
323 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
324 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
325 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
326 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
327 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
328 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
329 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
330 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
331 case WXK_ADD
: key
= "ADD"; break;
332 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
333 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
334 case WXK_DECIMAL
: key
= "DECIMAL"; break;
335 case WXK_DIVIDE
: key
= "DIVIDE"; break;
336 case WXK_F1
: key
= "F1"; break;
337 case WXK_F2
: key
= "F2"; break;
338 case WXK_F3
: key
= "F3"; break;
339 case WXK_F4
: key
= "F4"; break;
340 case WXK_F5
: key
= "F5"; break;
341 case WXK_F6
: key
= "F6"; break;
342 case WXK_F7
: key
= "F7"; break;
343 case WXK_F8
: key
= "F8"; break;
344 case WXK_F9
: key
= "F9"; break;
345 case WXK_F10
: key
= "F10"; break;
346 case WXK_F11
: key
= "F11"; break;
347 case WXK_F12
: key
= "F12"; break;
348 case WXK_F13
: key
= "F13"; break;
349 case WXK_F14
: key
= "F14"; break;
350 case WXK_F15
: key
= "F15"; break;
351 case WXK_F16
: key
= "F16"; break;
352 case WXK_F17
: key
= "F17"; break;
353 case WXK_F18
: key
= "F18"; break;
354 case WXK_F19
: key
= "F19"; break;
355 case WXK_F20
: key
= "F20"; break;
356 case WXK_F21
: key
= "F21"; break;
357 case WXK_F22
: key
= "F22"; break;
358 case WXK_F23
: key
= "F23"; break;
359 case WXK_F24
: key
= "F24"; break;
360 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
361 case WXK_SCROLL
: key
= "SCROLL"; break;
362 case WXK_PAGEUP
: key
= "PAGEUP"; break;
363 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
364 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
365 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
366 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
367 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
368 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
369 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
370 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
371 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
372 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
373 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
374 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
375 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
376 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
377 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
378 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
379 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
380 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
381 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
382 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
383 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
384 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
385 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
386 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
387 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
388 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
392 if ( wxIsprint((int)keycode
) )
393 key
.Printf( _T("'%c'") , (char)keycode
);
395 key
.Printf( _T("unknown (%ld)"), keycode
);
400 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
403 GetChar( event
.ControlDown(), _T('C') ),
404 GetChar( event
.AltDown(), _T('A') ),
405 GetChar( event
.ShiftDown(), _T('S') ),
406 GetChar( event
.MetaDown(), _T('M') ) );
409 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
416 msg
= _T("Mouse entered the window");
418 else if ( ev
.Leaving() )
420 msg
= _T("Mouse left the window");
427 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
430 dbl
= ev
.LeftDClick();
433 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
435 button
= _T("Middle");
436 dbl
= ev
.MiddleDClick();
439 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
441 button
= _T("Right");
442 dbl
= ev
.RightDClick();
447 wxLogStatus(_T("Unknown mouse event"));
451 msg
.Printf(_T("%s mouse button %s"),
453 dbl
? _T("double clicked")
454 : up
? _T("released") : _T("clicked"));
457 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ")
459 << GetChar( ev
.LeftIsDown(), _T('1') )
460 << GetChar( ev
.MiddleIsDown(), _T('2') )
461 << GetChar( ev
.RightIsDown(), _T('3') )
462 << GetChar( ev
.ControlDown(), _T('C') )
463 << GetChar( ev
.AltDown(), _T('A') )
464 << GetChar( ev
.ShiftDown(), _T('S') )
465 << GetChar( ev
.MetaDown(), _T('M') );
469 //else: we're not interested in mouse move events
474 void MyTextCtrl::OnText(wxCommandEvent
& event
)
476 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
477 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
480 wxLogMessage(_T("Text changed in control '%s'"), data
);
484 wxLogMessage(_T("Text changed in some control"));
488 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
490 LogEvent( _T("Char"), event
);
492 /* How are we supposed to test wxTE_PROCESS_TAB with this code?
494 if ( event.KeyCode() == WXK_TAB )
506 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
508 LogEvent( _T("Key up"), event
);
513 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
515 switch ( event
.KeyCode() )
518 // show current position and text length
520 long line
, column
, pos
= GetInsertionPoint();
521 PositionToXY(pos
, &column
, &line
);
523 wxLogMessage( _T("Current position: %ld\n"
524 "Current line, column: (%ld, %ld)\n"
525 "Number of lines: %ld\n"
526 "Current line length: %ld\n"
527 "Total text length: %u (%ld)"),
539 SetInsertionPointEnd();
544 SetInsertionPoint(10);
550 wxLogDebug( wxT("Now capturing mouse and events.") );
556 wxLogDebug( wxT("Stopped capturing mouse and events.") );
563 // insert a blank line
568 SetValue("F6 was just pressed.");
576 LogEvent( wxT("Key down"), event
);
581 //----------------------------------------------------------------------
583 //----------------------------------------------------------------------
585 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
586 EVT_SIZE(MyPanel::OnSize
)
589 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
590 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
592 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE
);
594 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
598 // single line text controls
600 m_text
= new MyTextCtrl( this, -1, "Single line.",
601 wxPoint(10,10), wxSize(140,-1),
603 m_text
->SetForegroundColour(*wxBLUE
);
604 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
605 (*m_text
) << " Appended.";
606 m_text
->SetInsertionPoint(0);
607 m_text
->WriteText( "Prepended. " );
609 m_password
= new MyTextCtrl( this, -1, "",
610 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
612 m_readonly
= new MyTextCtrl( this, -1, "Read only",
613 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
615 // multi line text controls
617 m_horizontal
= new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
618 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
620 // a little hack to use the command line argument for encoding testing
621 if ( wxTheApp
->argc
== 2 )
623 switch ( wxTheApp
->argv
[1][0] )
626 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
628 wxFONTENCODING_ISO8859_2
));
629 m_horizontal
->SetValue("®lu»ouèký kùò zbìsile èe¹tina «»");
633 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
635 wxFONTENCODING_KOI8
));
636 m_horizontal
->SetValue("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
641 m_horizontal
->SetValue("Text in default encoding");
644 m_multitext
= new MyTextCtrl( this, -1, "Multi line.",
645 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
646 m_multitext
->SetFont(*wxITALIC_FONT
);
647 (*m_multitext
) << " Appended.";
648 m_multitext
->SetInsertionPoint(0);
649 m_multitext
->WriteText( "Prepended. " );
650 m_multitext
->SetForegroundColour(*wxRED
);
651 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
654 m_multitext
->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
657 m_tab
= new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
658 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
659 m_tab
->SetClientData((void *)_T("tab"));
661 m_enter
= new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
662 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
663 m_enter
->SetClientData((void *)_T("enter"));
665 m_textrich
= new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
666 "(even under broken Win9x)\n"
667 "and a very very very very very "
668 "very very very long line to test"
670 wxPoint(450, 10), wxSize(230, 230),
671 wxTE_RICH
| wxTE_MULTILINE
| wxHSCROLL
);
673 m_textrich
->SetStyle(0, 10, *wxRED
);
674 m_textrich
->SetStyle(10, 20, *wxBLUE
);
675 m_textrich
->SetStyle(30, 40,
676 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
677 m_textrich
->SetDefaultStyle(wxTextAttr());
678 m_textrich
->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
679 m_textrich
->AppendText(_T("Next 10 characters should be in blue\n"));
680 m_textrich
->AppendText(_T("Next 10 characters should be normal\n"));
681 m_textrich
->AppendText(_T("And the next 10 characters should be green and italic\n"));
682 m_textrich
->SetDefaultStyle(wxTextAttr(*wxCYAN
, *wxBLUE
));
683 m_textrich
->AppendText(_T("This text should be cyan on blue\n"));
684 m_textrich
->SetDefaultStyle(*wxBLUE
);
685 m_textrich
->AppendText(_T("And this should be in blue and the text you ")
686 _T("type should be in blue as well"));
689 void MyPanel::OnSize( wxSizeEvent
&event
)
691 wxSize
client_area( GetClientSize() );
692 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
697 void MyPanel::DoPasteFromClipboard()
699 // On X11, we want to get the data from the primary selection instead
700 // of the normal clipboard (which isn't normal under X11 at all). This
701 // call has no effect under MSW.
702 wxTheClipboard
->UsePrimarySelection();
704 if (!wxTheClipboard
->Open())
706 *m_log
<< "Error opening the clipboard.\n";
711 *m_log
<< "Successfully opened the clipboard.\n";
714 wxTextDataObject data
;
716 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
718 *m_log
<< "Clipboard supports requested format.\n";
720 if (wxTheClipboard
->GetData( data
))
722 *m_log
<< "Successfully retrieved data from the clipboard.\n";
723 *m_multitext
<< data
.GetText() << "\n";
727 *m_log
<< "Error getting data from the clipboard.\n";
732 *m_log
<< "Clipboard doesn't support requested format.\n";
735 wxTheClipboard
->Close();
737 *m_log
<< "Closed the clipboard.\n";
740 void MyPanel::DoCopyToClipboard()
742 // On X11, we want to get the data from the primary selection instead
743 // of the normal clipboard (which isn't normal under X11 at all). This
744 // call has no effect under MSW.
745 wxTheClipboard
->UsePrimarySelection();
747 wxString
text( m_multitext
->GetLineText(0) );
751 *m_log
<< "No text to copy.\n";
756 if (!wxTheClipboard
->Open())
758 *m_log
<< "Error opening the clipboard.\n";
764 *m_log
<< "Successfully opened the clipboard.\n";
767 wxTextDataObject
*data
= new wxTextDataObject( text
);
769 if (!wxTheClipboard
->SetData( data
))
771 *m_log
<< "Error while copying to the clipboard.\n";
775 *m_log
<< "Successfully copied data to the clipboard.\n";
778 wxTheClipboard
->Close();
780 *m_log
<< "Closed the clipboard.\n";
783 #endif // wxUSE_CLIPBOARD
785 void MyPanel::DoMoveToEndOfText()
787 m_multitext
->SetInsertionPointEnd();
788 m_multitext
->SetFocus();
791 void MyPanel::DoMoveToEndOfEntry()
793 m_text
->SetInsertionPointEnd();
797 //----------------------------------------------------------------------
799 //----------------------------------------------------------------------
801 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
802 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
803 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
804 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
805 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
806 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
809 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
810 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
811 #endif // wxUSE_TOOLTIPS
814 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
815 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
816 #endif // wxUSE_CLIPBOARD
818 EVT_MENU(TEXT_ADD_SOME
, MyFrame::OnAddText
)
819 EVT_MENU(TEXT_ADD_FREEZE
, MyFrame::OnAddTextFreeze
)
820 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
821 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
823 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
824 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
826 EVT_IDLE(MyFrame::OnIdle
)
829 MyFrame::MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
)
830 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
) )
834 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
837 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
842 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
846 wxMessageDialog
dialog(this,
847 "This is a text control sample. It demonstrates the many different\n"
848 "text control styles, the use of the clipboard, setting and handling\n"
849 "tooltips and intercepting key and char events.\n"
851 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
852 "About wxTextCtrl Sample",
853 wxOK
| wxICON_INFORMATION
);
861 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
863 static long s_delay
= 5000;
866 delay
.Printf( _T("%ld"), s_delay
);
868 delay
= wxGetTextFromUser("Enter delay (in milliseconds)",
875 wxSscanf(delay
, _T("%ld"), &s_delay
);
877 wxToolTip::SetDelay(s_delay
);
879 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
882 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
884 static bool s_enabled
= TRUE
;
886 s_enabled
= !s_enabled
;
888 wxToolTip::Enable(s_enabled
);
890 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
894 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
896 m_panel
->m_log
->Clear();
899 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
901 static bool s_editable
= TRUE
;
903 s_editable
= !s_editable
;
904 m_panel
->m_text
->SetEditable(s_editable
);
905 m_panel
->m_password
->SetEditable(s_editable
);
906 m_panel
->m_multitext
->SetEditable(s_editable
);
907 m_panel
->m_textrich
->SetEditable(s_editable
);
910 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
912 bool enabled
= m_panel
->m_text
->IsEnabled();
915 m_panel
->m_text
->Enable(enabled
);
916 m_panel
->m_password
->Enable(enabled
);
917 m_panel
->m_multitext
->Enable(enabled
);
918 m_panel
->m_readonly
->Enable(enabled
);
919 m_panel
->m_textrich
->Enable(enabled
);
922 void MyFrame::OnFileSave(wxCommandEvent
& event
)
924 if ( m_panel
->m_textrich
->SaveFile("dummy.txt") )
927 // verify that the fil length is correct (it wasn't under Win95)
928 wxFile
file("dummy.txt");
929 wxLogStatus(this, _T("Successfully saved file "
930 "(text len = %ld, file size = %ld)"),
931 m_panel
->m_textrich
->GetValue().length(),
936 wxLogStatus(this, _T("Couldn't save the file"));
939 void MyFrame::OnFileLoad(wxCommandEvent
& event
)
941 if ( m_panel
->m_textrich
->LoadFile("dummy.txt") )
942 wxLogStatus(this, _T("Successfully loaded file"));
944 wxLogStatus(this, _T("Couldn't load the file"));
947 void MyFrame::OnIdle( wxIdleEvent
& event
)
949 // track the window which has the focus in the status bar
950 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
951 wxWindow
*focus
= wxWindow::FindFocus();
952 if ( focus
&& (focus
!= s_windowFocus
) )
954 s_windowFocus
= focus
;
959 _T("Focus: wxWindow = %p, HWND = %08x"),
961 _T("Focus: wxWindow = %p"),
965 , s_windowFocus
->GetHWND()