ee6a8387ef9740f6d70e97637be0c1330eb55097
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: TextCtrl wxWindows sample
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling, Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "controls.h"
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"
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 //----------------------------------------------------------------------
45 //----------------------------------------------------------------------
47 class MyApp
: public wxApp
53 // a text ctrl which allows to call different wxTextCtrl functions
54 // interactively by pressing function keys in it
55 class MyTextCtrl
: public wxTextCtrl
58 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
59 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
60 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
) { m_hasCapture
= FALSE
; }
62 void OnKeyDown(wxKeyEvent
& event
);
63 void OnKeyUp(wxKeyEvent
& event
);
64 void OnChar(wxKeyEvent
& event
);
69 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
70 void LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
75 class MyPanel
: public wxPanel
78 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
81 void DoPasteFromClipboard();
82 void DoCopyToClipboard();
83 #endif // wxUSE_CLIPBOARD
85 void DoMoveToEndOfText();
86 void DoMoveToEndOfEntry();
88 void OnSize( wxSizeEvent
&event
);
91 MyTextCtrl
*m_password
;
94 MyTextCtrl
*m_readonly
;
96 MyTextCtrl
*m_multitext
;
97 MyTextCtrl
*m_horizontal
;
102 DECLARE_EVENT_TABLE()
105 class MyFrame
: public wxFrame
108 MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
);
110 void OnQuit(wxCommandEvent
& event
);
111 void OnAbout(wxCommandEvent
& event
);
113 void OnSetTooltipDelay(wxCommandEvent
& event
);
114 void OnToggleTooltips(wxCommandEvent
& event
);
115 #endif // wxUSE_TOOLTIPS
118 void OnPasteFromClipboard( wxCommandEvent
&event
)
119 { m_panel
->DoPasteFromClipboard(); }
120 void OnCopyToClipboard( wxCommandEvent
&event
)
121 { m_panel
->DoCopyToClipboard(); }
122 #endif // wxUSE_CLIPBOARD
124 void OnMoveToEndOfText( wxCommandEvent
&event
)
125 { m_panel
->DoMoveToEndOfText(); }
126 void OnMoveToEndOfEntry( wxCommandEvent
&event
)
127 { m_panel
->DoMoveToEndOfEntry(); }
129 void OnFileLoad(wxCommandEvent
& event
);
131 void OnIdle( wxIdleEvent
& event
);
136 DECLARE_EVENT_TABLE()
139 //----------------------------------------------------------------------
141 //----------------------------------------------------------------------
145 //----------------------------------------------------------------------
147 //----------------------------------------------------------------------
156 TEXT_CLIPBOARD_COPY
= 200,
157 TEXT_CLIPBOARD_PASTE
,
160 TEXT_TOOLTIPS_SETDELAY
= 300,
161 TEXT_TOOLTIPS_ENABLE
,
164 TEXT_MOVE_ENDTEXT
= 400,
170 // Create the main frame window
171 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
172 "Text wxWindows sample", 50, 50, 640, 420);
173 frame
->SetSizeHints( 500, 400 );
175 wxMenu
*file_menu
= new wxMenu
;
176 file_menu
->Append(TEXT_LOAD
, "&Load file\tCtrl-O",
177 "Load the sample file into text control");
178 file_menu
->AppendSeparator();
179 file_menu
->Append(TEXT_ABOUT
, "&About\tAlt-A");
180 file_menu
->AppendSeparator();
181 file_menu
->Append(TEXT_QUIT
, "E&xit\tAlt-X", "Quit controls sample");
183 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
184 menu_bar
->Append(file_menu
, "&File");
187 wxMenu
*tooltip_menu
= new wxMenu
;
188 tooltip_menu
->Append(TEXT_TOOLTIPS_SETDELAY
, "Set &delay\tCtrl-D");
189 tooltip_menu
->AppendSeparator();
190 tooltip_menu
->Append(TEXT_TOOLTIPS_ENABLE
, "&Toggle tooltips\tCtrl-T",
191 "enable/disable tooltips", TRUE
);
192 tooltip_menu
->Check(TEXT_TOOLTIPS_ENABLE
, TRUE
);
193 menu_bar
->Append(tooltip_menu
, "&Tooltips");
194 #endif // wxUSE_TOOLTIPS
197 wxMenu
*menuClipboard
= new wxMenu
;
198 menuClipboard
->Append(TEXT_CLIPBOARD_COPY
, "&Copy\tCtrl-C",
199 "Copy the first line to the clipboard");
200 menuClipboard
->Append(TEXT_CLIPBOARD_PASTE
, "&Paste\tCtrl-V",
201 "Paste from clipboard to the text control");
202 menu_bar
->Append(menuClipboard
, "&Clipboard");
203 #endif // wxUSE_CLIPBOARD
205 wxMenu
*menuMove
= new wxMenu
;
206 menuMove
->Append(TEXT_MOVE_ENDTEXT
, "To the end of &text");
207 menuMove
->Append(TEXT_MOVE_ENDENTRY
, "To the end of &entry");
208 menu_bar
->Append(menuMove
, "&Move");
210 frame
->SetMenuBar(menu_bar
);
220 //----------------------------------------------------------------------
222 //----------------------------------------------------------------------
224 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
225 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
226 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
227 EVT_CHAR(MyTextCtrl::OnChar
)
230 void MyTextCtrl::LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const
233 long keycode
= event
.KeyCode();
237 case WXK_BACK
: key
= "BACK"; break;
238 case WXK_TAB
: key
= "TAB"; break;
239 case WXK_RETURN
: key
= "RETURN"; break;
240 case WXK_ESCAPE
: key
= "ESCAPE"; break;
241 case WXK_SPACE
: key
= "SPACE"; break;
242 case WXK_DELETE
: key
= "DELETE"; break;
243 case WXK_START
: key
= "START"; break;
244 case WXK_LBUTTON
: key
= "LBUTTON"; break;
245 case WXK_RBUTTON
: key
= "RBUTTON"; break;
246 case WXK_CANCEL
: key
= "CANCEL"; break;
247 case WXK_MBUTTON
: key
= "MBUTTON"; break;
248 case WXK_CLEAR
: key
= "CLEAR"; break;
249 case WXK_SHIFT
: key
= "SHIFT"; break;
250 case WXK_ALT
: key
= "ALT"; break;
251 case WXK_CONTROL
: key
= "CONTROL"; break;
252 case WXK_MENU
: key
= "MENU"; break;
253 case WXK_PAUSE
: key
= "PAUSE"; break;
254 case WXK_CAPITAL
: key
= "CAPITAL"; break;
255 case WXK_PRIOR
: key
= "PRIOR"; break;
256 case WXK_NEXT
: key
= "NEXT"; break;
257 case WXK_END
: key
= "END"; break;
258 case WXK_HOME
: key
= "HOME"; break;
259 case WXK_LEFT
: key
= "LEFT"; break;
260 case WXK_UP
: key
= "UP"; break;
261 case WXK_RIGHT
: key
= "RIGHT"; break;
262 case WXK_DOWN
: key
= "DOWN"; break;
263 case WXK_SELECT
: key
= "SELECT"; break;
264 case WXK_PRINT
: key
= "PRINT"; break;
265 case WXK_EXECUTE
: key
= "EXECUTE"; break;
266 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
267 case WXK_INSERT
: key
= "INSERT"; break;
268 case WXK_HELP
: key
= "HELP"; break;
269 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
270 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
271 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
272 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
273 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
274 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
275 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
276 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
277 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
278 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
279 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
280 case WXK_ADD
: key
= "ADD"; break;
281 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
282 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
283 case WXK_DECIMAL
: key
= "DECIMAL"; break;
284 case WXK_DIVIDE
: key
= "DIVIDE"; break;
285 case WXK_F1
: key
= "F1"; break;
286 case WXK_F2
: key
= "F2"; break;
287 case WXK_F3
: key
= "F3"; break;
288 case WXK_F4
: key
= "F4"; break;
289 case WXK_F5
: key
= "F5"; break;
290 case WXK_F6
: key
= "F6"; break;
291 case WXK_F7
: key
= "F7"; break;
292 case WXK_F8
: key
= "F8"; break;
293 case WXK_F9
: key
= "F9"; break;
294 case WXK_F10
: key
= "F10"; break;
295 case WXK_F11
: key
= "F11"; break;
296 case WXK_F12
: key
= "F12"; break;
297 case WXK_F13
: key
= "F13"; break;
298 case WXK_F14
: key
= "F14"; break;
299 case WXK_F15
: key
= "F15"; break;
300 case WXK_F16
: key
= "F16"; break;
301 case WXK_F17
: key
= "F17"; break;
302 case WXK_F18
: key
= "F18"; break;
303 case WXK_F19
: key
= "F19"; break;
304 case WXK_F20
: key
= "F20"; break;
305 case WXK_F21
: key
= "F21"; break;
306 case WXK_F22
: key
= "F22"; break;
307 case WXK_F23
: key
= "F23"; break;
308 case WXK_F24
: key
= "F24"; break;
309 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
310 case WXK_SCROLL
: key
= "SCROLL"; break;
311 case WXK_PAGEUP
: key
= "PAGEUP"; break;
312 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
313 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
314 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
315 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
316 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
317 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
318 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
319 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
320 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
321 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
322 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
323 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
324 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
325 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
326 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
327 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
328 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
329 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
330 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
331 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
332 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
333 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
334 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
335 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
336 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
337 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
341 if ( wxIsprint((int)keycode
) )
342 key
.Printf( _T("'%c'") , (char)keycode
);
344 key
.Printf( _T("unknown (%ld)"), keycode
);
349 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
352 GetChar( event
.ControlDown(), _T('C') ),
353 GetChar( event
.AltDown(), _T('A') ),
354 GetChar( event
.ShiftDown(), _T('S') ),
355 GetChar( event
.MetaDown(), _T('M') ) );
359 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
361 LogEvent( _T("Char"), event
);
363 /* How are we supposed to test wxTE_PROCESS_TAB with this code?
365 if ( event.KeyCode() == WXK_TAB )
377 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
379 LogEvent( _("Key up"), event
);
384 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
386 switch ( event
.KeyCode() )
389 // show current position and text length
391 long line
, column
, pos
= GetInsertionPoint();
392 PositionToXY(pos
, &column
, &line
);
394 wxLogMessage( _T("Current position: %ld\n"
395 "Current line, column: (%ld, %ld)\n"
396 "Number of lines: %ld\n"
397 "Current line length: %ld\n"
398 "Total text length: %ld"),
409 SetInsertionPointEnd();
414 SetInsertionPoint(10);
420 wxLogDebug( wxT("Now capturing mouse and events.") );
426 wxLogDebug( wxT("Stopped capturing mouse and events.") );
433 LogEvent( _("Key down"), event
);
438 //----------------------------------------------------------------------
440 //----------------------------------------------------------------------
442 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
443 EVT_SIZE(MyPanel::OnSize
)
446 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
447 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
449 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE
);
451 wxLog
*old_log
= wxLog::SetActiveTarget( new wxLogTextCtrl( m_log
) );
455 // single line text controls
457 m_text
= new MyTextCtrl( this, -1, "Single line.",
458 wxPoint(10,10), wxSize(140,-1), 0);
459 (*m_text
) << " Appended.";
460 m_text
->SetInsertionPoint(0);
461 m_text
->WriteText( "Prepended. " );
463 m_password
= new MyTextCtrl( this, -1, "",
464 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
466 m_readonly
= new MyTextCtrl( this, -1, "Read only",
467 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
469 // multi line text controls
471 m_horizontal
= new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
472 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
473 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxNORMAL
,
474 FALSE
, "", wxFONTENCODING_KOI8
));
476 m_multitext
= new MyTextCtrl( this, -1, "Multi line.",
477 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
478 (*m_multitext
) << " Appended.";
479 m_multitext
->SetInsertionPoint(0);
480 m_multitext
->WriteText( "Prepended. " );
483 m_multitext
->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
486 m_tab
= new MyTextCtrl( this, -1, "Multiline, allow <TAB> processing.",
487 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
489 m_enter
= new MyTextCtrl( this, -1, "Multiline, allow <ENTER> processing.",
490 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
);
493 void MyPanel::OnSize( wxSizeEvent
&event
)
495 wxSize
client_area( GetClientSize() );
496 m_log
->SetSize( 0, 260, client_area
.x
, client_area
.y
- 260 );
501 void MyPanel::DoPasteFromClipboard()
503 // On X11, we want to get the data from the primary selection instead
504 // of the normal clipboard (which isn't normal under X11 at all). This
505 // call has no effect under MSW.
506 wxTheClipboard
->UsePrimarySelection();
508 if (!wxTheClipboard
->Open())
510 *m_log
<< "Error opening the clipboard.\n";
515 *m_log
<< "Successfully opened the clipboard.\n";
518 wxTextDataObject data
;
520 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
522 *m_log
<< "Clipboard supports requested format.\n";
524 if (wxTheClipboard
->GetData( data
))
526 *m_log
<< "Successfully retrieved data from the clipboard.\n";
527 *m_multitext
<< data
.GetText() << "\n";
531 *m_log
<< "Error getting data from the clipboard.\n";
536 *m_log
<< "Clipboard doesn't support requested format.\n";
539 wxTheClipboard
->Close();
541 *m_log
<< "Closed the clipboard.\n";
544 void MyPanel::DoCopyToClipboard()
546 wxString
text( m_multitext
->GetLineText(0) );
550 *m_log
<< "No text to copy.\n";
555 if (!wxTheClipboard
->Open())
557 *m_log
<< "Error opening the clipboard.\n";
563 *m_log
<< "Successfully opened the clipboard.\n";
566 wxTextDataObject
*data
= new wxTextDataObject( text
);
568 if (!wxTheClipboard
->SetData( data
))
570 *m_log
<< "Error while copying to the clipboard.\n";
574 *m_log
<< "Successfully copied data to the clipboard.\n";
577 wxTheClipboard
->Close();
579 *m_log
<< "Closed the clipboard.\n";
582 #endif // wxUSE_CLIPBOARD
584 void MyPanel::DoMoveToEndOfText()
586 m_multitext
->SetInsertionPointEnd();
587 m_multitext
->SetFocus();
590 void MyPanel::DoMoveToEndOfEntry()
592 m_text
->SetInsertionPointEnd();
596 //----------------------------------------------------------------------
598 //----------------------------------------------------------------------
600 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
601 EVT_MENU(TEXT_QUIT
, MyFrame::OnQuit
)
602 EVT_MENU(TEXT_ABOUT
, MyFrame::OnAbout
)
603 EVT_MENU(TEXT_LOAD
, MyFrame::OnFileLoad
)
606 EVT_MENU(TEXT_TOOLTIPS_SETDELAY
, MyFrame::OnSetTooltipDelay
)
607 EVT_MENU(TEXT_TOOLTIPS_ENABLE
, MyFrame::OnToggleTooltips
)
608 #endif // wxUSE_TOOLTIPS
611 EVT_MENU(TEXT_CLIPBOARD_PASTE
, MyFrame::OnPasteFromClipboard
)
612 EVT_MENU(TEXT_CLIPBOARD_COPY
, MyFrame::OnCopyToClipboard
)
613 #endif // wxUSE_CLIPBOARD
615 EVT_MENU(TEXT_MOVE_ENDTEXT
, MyFrame::OnMoveToEndOfText
)
616 EVT_MENU(TEXT_MOVE_ENDENTRY
, MyFrame::OnMoveToEndOfEntry
)
618 EVT_IDLE(MyFrame::OnIdle
)
621 MyFrame::MyFrame(wxFrame
*frame
, const char *title
, int x
, int y
, int w
, int h
)
622 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
) )
626 m_panel
= new MyPanel( this, 10, 10, 300, 100 );
629 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
634 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
638 wxMessageDialog
dialog(this,
639 "This is a text control sample. It demonstrates the many different\n"
640 "text control styles, the use of the clipboard, setting and handling\n"
641 "tooltips and intercepting key and char events.\n"
643 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
644 "About Text Controls",
645 wxOK
| wxICON_INFORMATION
);
653 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
655 static long s_delay
= 5000;
658 delay
.Printf( _T("%ld"), s_delay
);
660 delay
= wxGetTextFromUser("Enter delay (in milliseconds)",
667 wxSscanf(delay
, _T("%ld"), &s_delay
);
669 wxToolTip::SetDelay(s_delay
);
671 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
674 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
676 static bool s_enabled
= TRUE
;
678 s_enabled
= !s_enabled
;
680 wxToolTip::Enable(s_enabled
);
682 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
686 void MyFrame::OnFileLoad(wxCommandEvent
& event
)
688 if ( m_panel
->m_multitext
->LoadFile("text.rc") )
689 wxLogStatus(this, _T("Successfully loaded file"));
691 wxLogStatus(this, _T("Couldn't load the file"));
694 void MyFrame::OnIdle( wxIdleEvent
& event
)
696 // track the window which has the focus in the status bar
697 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
698 wxWindow
*focus
= wxWindow::FindFocus();
699 if ( focus
&& (focus
!= s_windowFocus
) )
701 s_windowFocus
= focus
;
706 _T("Focus: wxWindow = %p, HWND = %08x"),
708 _T("Focus: wxWindow = %p"),
712 , s_windowFocus
->GetHWND()