]>
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 OnMoveToEndOfText( wxCommandEvent
&event
)
133 { m_panel
->DoMoveToEndOfText(); }
134 void OnMoveToEndOfEntry( wxCommandEvent
&event
)
135 { m_panel
->DoMoveToEndOfEntry(); }
137 void OnLogClear(wxCommandEvent
& event
);
138 void OnFileSave(wxCommandEvent
& event
);
139 void OnFileLoad(wxCommandEvent
& event
);
141 void OnSetEditable(wxCommandEvent
& event
);
142 void OnSetEnabled(wxCommandEvent
& event
);
144 void OnIdle( wxIdleEvent
& event
);
149 DECLARE_EVENT_TABLE()
152 //----------------------------------------------------------------------
154 //----------------------------------------------------------------------
158 //----------------------------------------------------------------------
160 //----------------------------------------------------------------------
171 TEXT_CLIPBOARD_COPY
= 200,
172 TEXT_CLIPBOARD_PASTE
,
175 TEXT_TOOLTIPS_SETDELAY
= 300,
176 TEXT_TOOLTIPS_ENABLE
,
179 TEXT_MOVE_ENDTEXT
= 400,
187 // Create the main frame window
188 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
189 "Text wxWindows sample", 50, 50, 660, 420);
190 frame
->SetSizeHints( 500, 400 );
192 wxMenu
*file_menu
= new wxMenu
;
193 file_menu
->Append(TEXT_CLEAR
, "&Clear the log\tCtrl-C",
194 "Clear the log window contents");
195 file_menu
->Append(TEXT_SAVE
, "&Save file\tCtrl-S",
196 "Save the text control contents to file");
197 file_menu
->Append(TEXT_LOAD
, "&Load file\tCtrl-O",
198 "Load the sample file into text control");
199 file_menu
->AppendSeparator();
200 file_menu
->Append(TEXT_ABOUT
, "&About\tAlt-A");
201 file_menu
->AppendSeparator();
202 file_menu
->Append(TEXT_QUIT
, "E&xit\tAlt-X", "Quit this sample");
204 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
205 menu_bar
->Append(file_menu
, "&File");
208 wxMenu
*tooltip_menu
= new wxMenu
;
209 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, "Set &delay\tCtrl-D");
210 tooltip_menu
->AppendSeparator();
211 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, "&Toggle tooltips\tCtrl-T",
212 "enable/disable tooltips", TRUE
);
213 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, TRUE
);
214 menu_bar
->Append(tooltip_menu
, "&Tooltips");
215 #endif // wxUSE_TOOLTIPS
218 wxMenu
*menuClipboard
= new wxMenu
;
219 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, "&Copy\tCtrl-C",
220 "Copy the first line to the clipboard");
221 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, "&Paste\tCtrl-V",
222 "Paste from clipboard to the text control");
223 menu_bar
->Append(menuClipboard
, "&Clipboard");
224 #endif // wxUSE_CLIPBOARD
226 wxMenu
*menuText
= new wxMenu
;
227 menuText
->Append(TEXT_MOVE_ENDTEXT
, "Move cursor to the end of &text");
228 menuText
->Append(TEXT_MOVE_ENDENTRY
, "Move cursor to the end of &entry");
229 menuText
->Append(TEXT_SET_EDITABLE
, "Toggle &editable state", "", TRUE
);
230 menuText
->Append(TEXT_SET_ENABLED
, "Toggle e&nabled state", "", TRUE
);
231 menuText
->Check(TEXT_SET_EDITABLE
, TRUE
);
232 menuText
->Check(TEXT_SET_ENABLED
, TRUE
);
233 menu_bar
->Append(menuText
, "&Text");
235 frame
->SetMenuBar(menu_bar
);
245 //----------------------------------------------------------------------
247 //----------------------------------------------------------------------
249 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
250 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
251 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
252 EVT_CHAR(MyTextCtrl::OnChar
)
253 EVT_TEXT(-1, MyTextCtrl::OnText
)
254 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent
)
257 void MyTextCtrl::LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const
260 long keycode
= event
.KeyCode();
264 case WXK_BACK
: key
= "BACK"; break;
265 case WXK_TAB
: key
= "TAB"; break;
266 case WXK_RETURN
: key
= "RETURN"; break;
267 case WXK_ESCAPE
: key
= "ESCAPE"; break;
268 case WXK_SPACE
: key
= "SPACE"; break;
269 case WXK_DELETE
: key
= "DELETE"; break;
270 case WXK_START
: key
= "START"; break;
271 case WXK_LBUTTON
: key
= "LBUTTON"; break;
272 case WXK_RBUTTON
: key
= "RBUTTON"; break;
273 case WXK_CANCEL
: key
= "CANCEL"; break;
274 case WXK_MBUTTON
: key
= "MBUTTON"; break;
275 case WXK_CLEAR
: key
= "CLEAR"; break;
276 case WXK_SHIFT
: key
= "SHIFT"; break;
277 case WXK_ALT
: key
= "ALT"; break;
278 case WXK_CONTROL
: key
= "CONTROL"; break;
279 case WXK_MENU
: key
= "MENU"; break;
280 case WXK_PAUSE
: key
= "PAUSE"; break;
281 case WXK_CAPITAL
: key
= "CAPITAL"; break;
282 case WXK_PRIOR
: key
= "PRIOR"; break;
283 case WXK_NEXT
: key
= "NEXT"; break;
284 case WXK_END
: key
= "END"; break;
285 case WXK_HOME
: key
= "HOME"; break;
286 case WXK_LEFT
: key
= "LEFT"; break;
287 case WXK_UP
: key
= "UP"; break;
288 case WXK_RIGHT
: key
= "RIGHT"; break;
289 case WXK_DOWN
: key
= "DOWN"; break;
290 case WXK_SELECT
: key
= "SELECT"; break;
291 case WXK_PRINT
: key
= "PRINT"; break;
292 case WXK_EXECUTE
: key
= "EXECUTE"; break;
293 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
294 case WXK_INSERT
: key
= "INSERT"; break;
295 case WXK_HELP
: key
= "HELP"; break;
296 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
297 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
298 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
299 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
300 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
301 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
302 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
303 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
304 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
305 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
306 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
307 case WXK_ADD
: key
= "ADD"; break;
308 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
309 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
310 case WXK_DECIMAL
: key
= "DECIMAL"; break;
311 case WXK_DIVIDE
: key
= "DIVIDE"; break;
312 case WXK_F1
: key
= "F1"; break;
313 case WXK_F2
: key
= "F2"; break;
314 case WXK_F3
: key
= "F3"; break;
315 case WXK_F4
: key
= "F4"; break;
316 case WXK_F5
: key
= "F5"; break;
317 case WXK_F6
: key
= "F6"; break;
318 case WXK_F7
: key
= "F7"; break;
319 case WXK_F8
: key
= "F8"; break;
320 case WXK_F9
: key
= "F9"; break;
321 case WXK_F10
: key
= "F10"; break;
322 case WXK_F11
: key
= "F11"; break;
323 case WXK_F12
: key
= "F12"; break;
324 case WXK_F13
: key
= "F13"; break;
325 case WXK_F14
: key
= "F14"; break;
326 case WXK_F15
: key
= "F15"; break;
327 case WXK_F16
: key
= "F16"; break;
328 case WXK_F17
: key
= "F17"; break;
329 case WXK_F18
: key
= "F18"; break;
330 case WXK_F19
: key
= "F19"; break;
331 case WXK_F20
: key
= "F20"; break;
332 case WXK_F21
: key
= "F21"; break;
333 case WXK_F22
: key
= "F22"; break;
334 case WXK_F23
: key
= "F23"; break;
335 case WXK_F24
: key
= "F24"; break;
336 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
337 case WXK_SCROLL
: key
= "SCROLL"; break;
338 case WXK_PAGEUP
: key
= "PAGEUP"; break;
339 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
340 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
341 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
342 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
343 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
344 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
345 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
346 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
347 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
348 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
349 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
350 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
351 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
352 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
353 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
354 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
355 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
356 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
357 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
358 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
359 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
360 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
361 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
362 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
363 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
364 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
368 if ( wxIsprint((int)keycode
) )
369 key
.Printf( _T("'%c'") , (char)keycode
);
371 key
.Printf( _T("unknown (%ld)"), keycode
);
376 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
379 GetChar( event
.ControlDown(), _T('C') ),
380 GetChar( event
.AltDown(), _T('A') ),
381 GetChar( event
.ShiftDown(), _T('S') ),
382 GetChar( event
.MetaDown(), _T('M') ) );
385 void MyTextCtrl::OnMouseEvent(wxMouseEvent
& ev
)
392 msg
= _T("Mouse entered the window");
394 else if ( ev
.Leaving() )
396 msg
= _T("Mouse left the window");
403 if ( ev
.LeftDown() || ev
.LeftUp() || ev
.LeftDClick() )
406 dbl
= ev
.LeftDClick();
409 else if ( ev
.MiddleDown() || ev
.MiddleUp() || ev
.MiddleDClick() )
411 button
= _T("Middle");
412 dbl
= ev
.MiddleDClick();
415 else if ( ev
.RightDown() || ev
.RightUp() || ev
.RightDClick() )
417 button
= _T("Right");
418 dbl
= ev
.RightDClick();
423 wxLogStatus(_T("Unknown mouse event"));
427 msg
.Printf(_T("%s mouse button %s"),
429 dbl
? _T("double clicked")
430 : up
? _T("released") : _T("clicked"));
433 msg
<< _T(" at (") << ev
.GetX() << _T(", ") << ev
.GetY() << _T(") ")
435 << GetChar( ev
.LeftIsDown(), _T('1') )
436 << GetChar( ev
.MiddleIsDown(), _T('2') )
437 << GetChar( ev
.RightIsDown(), _T('3') )
438 << GetChar( ev
.ControlDown(), _T('C') )
439 << GetChar( ev
.AltDown(), _T('A') )
440 << GetChar( ev
.ShiftDown(), _T('S') )
441 << GetChar( ev
.MetaDown(), _T('M') );
445 //else: we're not interested in mouse move events
450 void MyTextCtrl::OnText(wxCommandEvent
& event
)
452 MyTextCtrl
*win
= (MyTextCtrl
*)event
.GetEventObject();
453 const wxChar
*data
= (const wxChar
*)(win
->GetClientData());
456 wxLogMessage(_T("Text changed in control '%s'"), data
);
460 wxLogMessage(_T("Text changed in some control"));
464 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
466 LogEvent( _T("Char"), event
);
468 /* How are we supposed to test wxTE_PROCESS_TAB with this code?
470 if ( event.KeyCode() == WXK_TAB )
482 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
484 LogEvent( _T("Key up"), event
);
489 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
491 switch ( event
.KeyCode() )
494 // show current position and text length
496 long line
, column
, pos
= GetInsertionPoint();
497 PositionToXY(pos
, &column
, &line
);
499 wxLogMessage( _T("Current position: %ld\n"
500 "Current line, column: (%ld, %ld)\n"
501 "Number of lines: %ld\n"
502 "Current line length: %ld\n"
503 "Total text length: %u (%ld)"),
515 SetInsertionPointEnd();
520 SetInsertionPoint(10);
526 wxLogDebug( wxT("Now capturing mouse and events.") );
532 wxLogDebug( wxT("Stopped capturing mouse and events.") );
539 // insert a blank line
544 SetValue("F6 was just pressed.");
552 LogEvent( wxT("Key down"), event
);
557 //----------------------------------------------------------------------
559 //----------------------------------------------------------------------
561 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
562 EVT_SIZE(MyPanel::OnSize
)
565 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
566 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
568 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE
);
570 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
574 // single line text controls
576 m_text
= new MyTextCtrl( this, -1, "Single line.",
577 wxPoint(10,10), wxSize(140,-1),
579 m_text
->SetForegroundColour(*wxBLUE
);
580 m_text
->SetBackgroundColour(*wxLIGHT_GREY
);
581 (*m_text
) << " Appended.";
582 m_text
->SetInsertionPoint(0);
583 m_text
->WriteText( "Prepended. " );
585 m_password
= new MyTextCtrl( this, -1, "",
586 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
588 m_readonly
= new MyTextCtrl( this, -1, "Read only",
589 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
591 // multi line text controls
593 m_horizontal
= new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
594 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
596 // a little hack to use the command line argument for encoding testing
597 if ( wxTheApp
->argc
== 2 )
599 switch ( wxTheApp
->argv
[1][0] )
602 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
604 wxFONTENCODING_ISO8859_2
));
605 m_horizontal
->SetValue("®lu»ouèký kùò zbìsile èe¹tina «»");
609 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
611 wxFONTENCODING_KOI8
));
612 m_horizontal
->SetValue("ËÁÖÅÔÓÑ ÕÄÁÞÎÙÍ");
617 m_horizontal
->SetValue("Text in default encoding");
620 m_multitext
= new MyTextCtrl( this, -1, "Multi line.",
621 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
622 m_multitext
->SetFont(*wxITALIC_FONT
);
623 (*m_multitext
) << " Appended.";
624 m_multitext
->SetInsertionPoint(0);
625 m_multitext
->WriteText( "Prepended. " );
626 m_multitext
->SetForegroundColour(*wxRED
);
627 m_multitext
->SetBackgroundColour(*wxLIGHT_GREY
);
630 m_multitext
->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
633 m_tab
= new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
634 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
635 m_tab
->SetClientData((void *)_T("tab"));
637 m_enter
= new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
638 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
639 m_enter
->SetClientData((void *)_T("enter"));
641 m_textrich
= new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
642 "(even under broken Win9x)\n"
643 "and a very very very very very "
644 "very very very long line to test"
646 wxPoint(450, 10), wxSize(200, 230),
647 wxTE_RICH
| wxTE_MULTILINE
| wxHSCROLL
);
649 m_textrich
->SetStyle(0, 10, *wxRED
);
650 m_textrich
->SetStyle(10, 20, *wxBLUE
);
651 m_textrich
->SetStyle(30, 40,
652 wxTextAttr(*wxGREEN
, wxNullColour
, *wxITALIC_FONT
));
653 m_textrich
->SetDefaultStyle(*wxBLUE
);
654 m_textrich
->AppendText(_T("\nIs this text really in blue?"));
656 m_textrich
->SetForegroundColour(wxColour(0, 255, 255));
657 m_textrich
->SetBackgroundColour(*wxBLUE
);
661 void MyPanel::OnSize( wxSizeEvent
&event
)
663 wxSize
client_area( GetClientSize() );
664 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
669 void MyPanel::DoPasteFromClipboard()
671 // On X11, we want to get the data from the primary selection instead
672 // of the normal clipboard (which isn't normal under X11 at all). This
673 // call has no effect under MSW.
674 wxTheClipboard
->UsePrimarySelection();
676 if (!wxTheClipboard
->Open())
678 *m_log
<< "Error opening the clipboard.\n";
683 *m_log
<< "Successfully opened the clipboard.\n";
686 wxTextDataObject data
;
688 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
690 *m_log
<< "Clipboard supports requested format.\n";
692 if (wxTheClipboard
->GetData( data
))
694 *m_log
<< "Successfully retrieved data from the clipboard.\n";
695 *m_multitext
<< data
.GetText() << "\n";
699 *m_log
<< "Error getting data from the clipboard.\n";
704 *m_log
<< "Clipboard doesn't support requested format.\n";
707 wxTheClipboard
->Close();
709 *m_log
<< "Closed the clipboard.\n";
712 void MyPanel::DoCopyToClipboard()
714 // On X11, we want to get the data from the primary selection instead
715 // of the normal clipboard (which isn't normal under X11 at all). This
716 // call has no effect under MSW.
717 wxTheClipboard
->UsePrimarySelection();
719 wxString
text( m_multitext
->GetLineText(0) );
723 *m_log
<< "No text to copy.\n";
728 if (!wxTheClipboard
->Open())
730 *m_log
<< "Error opening the clipboard.\n";
736 *m_log
<< "Successfully opened the clipboard.\n";
739 wxTextDataObject
*data
= new wxTextDataObject( text
);
741 if (!wxTheClipboard
->SetData( data
))
743 *m_log
<< "Error while copying to the clipboard.\n";
747 *m_log
<< "Successfully copied data to the clipboard.\n";
750 wxTheClipboard
->Close();
752 *m_log
<< "Closed the clipboard.\n";
755 #endif // wxUSE_CLIPBOARD
757 void MyPanel::DoMoveToEndOfText()
759 m_multitext
->SetInsertionPointEnd();
760 m_multitext
->SetFocus();
763 void MyPanel::DoMoveToEndOfEntry()
765 m_text
->SetInsertionPointEnd();
769 //----------------------------------------------------------------------
771 //----------------------------------------------------------------------
773 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
774 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
775 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
776 EVT_MENU(TEXT_SAVE
, MyFrame::OnFileSave
)
777 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
778 EVT_MENU(TEXT_CLEAR
, MyFrame::OnLogClear
)
781 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
782 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
783 #endif // wxUSE_TOOLTIPS
786 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
787 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
788 #endif // wxUSE_CLIPBOARD
790 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
791 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
793 EVT_MENU(TEXT_SET_EDITABLE
, MyFrame::OnSetEditable
)
794 EVT_MENU(TEXT_SET_ENABLED
, MyFrame::OnSetEnabled
)
796 EVT_IDLE(MyFrame::OnIdle
)
799 MyFrame::MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
)
800 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
) )
804 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
807 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
812 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
816 wxMessageDialog
dialog(this,
817 "This is a text control sample. It demonstrates the many different\n"
818 "text control styles, the use of the clipboard, setting and handling\n"
819 "tooltips and intercepting key and char events.\n"
821 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
822 "About wxTextCtrl Sample",
823 wxOK
| wxICON_INFORMATION
);
831 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
833 static long s_delay
= 5000;
836 delay
.Printf( _T("%ld"), s_delay
);
838 delay
= wxGetTextFromUser("Enter delay (in milliseconds)",
845 wxSscanf(delay
, _T("%ld"), &s_delay
);
847 wxToolTip::SetDelay(s_delay
);
849 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
852 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
854 static bool s_enabled
= TRUE
;
856 s_enabled
= !s_enabled
;
858 wxToolTip::Enable(s_enabled
);
860 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
864 void MyFrame::OnLogClear(wxCommandEvent
& WXUNUSED(event
))
866 m_panel
->m_log
->Clear();
869 void MyFrame::OnSetEditable(wxCommandEvent
& WXUNUSED(event
))
871 static bool s_editable
= TRUE
;
873 s_editable
= !s_editable
;
874 m_panel
->m_text
->SetEditable(s_editable
);
875 m_panel
->m_password
->SetEditable(s_editable
);
876 m_panel
->m_multitext
->SetEditable(s_editable
);
877 m_panel
->m_textrich
->SetEditable(s_editable
);
880 void MyFrame::OnSetEnabled(wxCommandEvent
& WXUNUSED(event
))
882 bool enabled
= m_panel
->m_text
->IsEnabled();
885 m_panel
->m_text
->Enable(enabled
);
886 m_panel
->m_password
->Enable(enabled
);
887 m_panel
->m_multitext
->Enable(enabled
);
888 m_panel
->m_readonly
->Enable(enabled
);
889 m_panel
->m_textrich
->Enable(enabled
);
892 void MyFrame::OnFileSave(wxCommandEvent
& event
)
894 if ( m_panel
->m_textrich
->SaveFile("dummy.txt") )
897 // verify that the fil length is correct (it wasn't under Win95)
898 wxFile
file("dummy.txt");
899 wxLogStatus(this, _T("Successfully saved file "
900 "(text len = %ld, file size = %ld)"),
901 m_panel
->m_textrich
->GetValue().length(),
906 wxLogStatus(this, _T("Couldn't save the file"));
909 void MyFrame::OnFileLoad(wxCommandEvent
& event
)
911 if ( m_panel
->m_textrich
->LoadFile("dummy.txt") )
912 wxLogStatus(this, _T("Successfully loaded file"));
914 wxLogStatus(this, _T("Couldn't load the file"));
917 void MyFrame::OnIdle( wxIdleEvent
& event
)
919 // track the window which has the focus in the status bar
920 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
921 wxWindow
*focus
= wxWindow::FindFocus();
922 if ( focus
&& (focus
!= s_windowFocus
) )
924 s_windowFocus
= focus
;
929 _T("Focus: wxWindow = %p, HWND = %08x"),
931 _T("Focus: wxWindow = %p"),
935 , s_windowFocus
->GetHWND()