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 #if defined(__WXGTK__) || defined(__WXMOTIF__)
36 #include "mondrian.xpm"
39 //----------------------------------------------------------------------
41 //----------------------------------------------------------------------
43 class MyApp
: public wxApp
49 // a text ctrl which allows to call different wxTextCtrl functions
50 // interactively by pressing function keys in it
51 class MyTextCtrl
: public wxTextCtrl
54 MyTextCtrl(wxWindow
*parent
, wxWindowID id
, const wxString
&value
,
55 const wxPoint
&pos
, const wxSize
&size
, int style
= 0)
56 : wxTextCtrl(parent
, id
, value
, pos
, size
, style
) { }
58 void OnKeyDown(wxKeyEvent
& event
);
59 void OnKeyUp(wxKeyEvent
& event
);
60 void OnChar(wxKeyEvent
& event
);
63 static inline wxChar
GetChar(bool on
, wxChar c
) { return on
? c
: _T('-'); }
64 void LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const;
69 class MyPanel
: public wxPanel
72 MyPanel(wxFrame
*frame
, int x
, int y
, int w
, int h
);
74 void OnPasteFromClipboard( wxCommandEvent
&event
);
75 void OnCopyToClipboard( wxCommandEvent
&event
);
76 void OnMoveToEndOfText( wxCommandEvent
&event
);
77 void OnMoveToEndOfEntry( wxCommandEvent
&event
);
80 MyTextCtrl
*m_password
;
83 MyTextCtrl
*m_entertab
;
84 MyTextCtrl
*m_readonly
;
86 MyTextCtrl
*m_multitext
;
87 MyTextCtrl
*m_horizontal
;
88 MyTextCtrl
*m_multitab
;
96 class MyFrame
: public wxFrame
99 MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
101 void OnQuit(wxCommandEvent
& event
);
102 void OnAbout(wxCommandEvent
& event
);
104 void OnSetTooltipDelay(wxCommandEvent
& event
);
105 void OnToggleTooltips(wxCommandEvent
& event
);
106 #endif // wxUSE_TOOLTIPS
107 void OnIdle( wxIdleEvent
& event
);
110 DECLARE_EVENT_TABLE()
113 //----------------------------------------------------------------------
115 //----------------------------------------------------------------------
119 //----------------------------------------------------------------------
121 //----------------------------------------------------------------------
130 MINIMAL_SET_TOOLTIP_DELAY
= 200,
131 MINIMAL_ENABLE_TOOLTIPS
136 // Create the main frame window
137 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
,
138 "Text wxWindows App",
142 // The wxICON() macros loads an icon from a resource under Windows
143 // and uses an #included XPM image under GTK+ and Motif
145 frame
->SetIcon( wxICON(mondrian
) );
147 wxMenu
*file_menu
= new wxMenu
;
148 file_menu
->Append(MINIMAL_ABOUT
, "&About\tAlt-A");
149 file_menu
->Append(MINIMAL_QUIT
, "E&xit\tAlt-X", "Quit controls sample");
151 wxMenuBar
*menu_bar
= new wxMenuBar( wxMB_DOCKABLE
);
152 menu_bar
->Append(file_menu
, "&File");
155 wxMenu
*tooltip_menu
= new wxMenu
;
156 tooltip_menu
->Append(MINIMAL_SET_TOOLTIP_DELAY
, "Set &delay\tCtrl-D");
157 tooltip_menu
->AppendSeparator();
158 tooltip_menu
->Append(MINIMAL_ENABLE_TOOLTIPS
, "&Toggle tooltips\tCrtl-T",
159 "enable/disable tooltips", TRUE
);
160 tooltip_menu
->Check(MINIMAL_ENABLE_TOOLTIPS
, TRUE
);
161 menu_bar
->Append(tooltip_menu
, "&Tooltips");
162 #endif // wxUSE_TOOLTIPS
164 frame
->SetMenuBar(menu_bar
);
174 //----------------------------------------------------------------------
176 //----------------------------------------------------------------------
178 BEGIN_EVENT_TABLE(MyTextCtrl
, wxTextCtrl
)
179 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown
)
180 EVT_KEY_UP(MyTextCtrl::OnKeyUp
)
181 EVT_CHAR(MyTextCtrl::OnChar
)
184 void MyTextCtrl::LogEvent(const wxChar
*name
, wxKeyEvent
& event
) const
187 long keycode
= event
.KeyCode();
188 if ( wxIsprint((int)keycode
) )
189 key
.Printf( _T("'%c'") , (char)keycode
);
194 case WXK_BACK
: key
= "BACK"; break;
195 case WXK_TAB
: key
= "TAB"; break;
196 case WXK_RETURN
: key
= "RETURN"; break;
197 case WXK_ESCAPE
: key
= "ESCAPE"; break;
198 case WXK_SPACE
: key
= "SPACE"; break;
199 case WXK_DELETE
: key
= "DELETE"; break;
200 case WXK_START
: key
= "START"; break;
201 case WXK_LBUTTON
: key
= "LBUTTON"; break;
202 case WXK_RBUTTON
: key
= "RBUTTON"; break;
203 case WXK_CANCEL
: key
= "CANCEL"; break;
204 case WXK_MBUTTON
: key
= "MBUTTON"; break;
205 case WXK_CLEAR
: key
= "CLEAR"; break;
206 case WXK_SHIFT
: key
= "SHIFT"; break;
207 case WXK_ALT
: key
= "ALT"; break;
208 case WXK_CONTROL
: key
= "CONTROL"; break;
209 case WXK_MENU
: key
= "MENU"; break;
210 case WXK_PAUSE
: key
= "PAUSE"; break;
211 case WXK_CAPITAL
: key
= "CAPITAL"; break;
212 case WXK_PRIOR
: key
= "PRIOR"; break;
213 case WXK_NEXT
: key
= "NEXT"; break;
214 case WXK_END
: key
= "END"; break;
215 case WXK_HOME
: key
= "HOME"; break;
216 case WXK_LEFT
: key
= "LEFT"; break;
217 case WXK_UP
: key
= "UP"; break;
218 case WXK_RIGHT
: key
= "RIGHT"; break;
219 case WXK_DOWN
: key
= "DOWN"; break;
220 case WXK_SELECT
: key
= "SELECT"; break;
221 case WXK_PRINT
: key
= "PRINT"; break;
222 case WXK_EXECUTE
: key
= "EXECUTE"; break;
223 case WXK_SNAPSHOT
: key
= "SNAPSHOT"; break;
224 case WXK_INSERT
: key
= "INSERT"; break;
225 case WXK_HELP
: key
= "HELP"; break;
226 case WXK_NUMPAD0
: key
= "NUMPAD0"; break;
227 case WXK_NUMPAD1
: key
= "NUMPAD1"; break;
228 case WXK_NUMPAD2
: key
= "NUMPAD2"; break;
229 case WXK_NUMPAD3
: key
= "NUMPAD3"; break;
230 case WXK_NUMPAD4
: key
= "NUMPAD4"; break;
231 case WXK_NUMPAD5
: key
= "NUMPAD5"; break;
232 case WXK_NUMPAD6
: key
= "NUMPAD6"; break;
233 case WXK_NUMPAD7
: key
= "NUMPAD7"; break;
234 case WXK_NUMPAD8
: key
= "NUMPAD8"; break;
235 case WXK_NUMPAD9
: key
= "NUMPAD9"; break;
236 case WXK_MULTIPLY
: key
= "MULTIPLY"; break;
237 case WXK_ADD
: key
= "ADD"; break;
238 case WXK_SEPARATOR
: key
= "SEPARATOR"; break;
239 case WXK_SUBTRACT
: key
= "SUBTRACT"; break;
240 case WXK_DECIMAL
: key
= "DECIMAL"; break;
241 case WXK_DIVIDE
: key
= "DIVIDE"; break;
242 case WXK_F1
: key
= "F1"; break;
243 case WXK_F2
: key
= "F2"; break;
244 case WXK_F3
: key
= "F3"; break;
245 case WXK_F4
: key
= "F4"; break;
246 case WXK_F5
: key
= "F5"; break;
247 case WXK_F6
: key
= "F6"; break;
248 case WXK_F7
: key
= "F7"; break;
249 case WXK_F8
: key
= "F8"; break;
250 case WXK_F9
: key
= "F9"; break;
251 case WXK_F10
: key
= "F10"; break;
252 case WXK_F11
: key
= "F11"; break;
253 case WXK_F12
: key
= "F12"; break;
254 case WXK_F13
: key
= "F13"; break;
255 case WXK_F14
: key
= "F14"; break;
256 case WXK_F15
: key
= "F15"; break;
257 case WXK_F16
: key
= "F16"; break;
258 case WXK_F17
: key
= "F17"; break;
259 case WXK_F18
: key
= "F18"; break;
260 case WXK_F19
: key
= "F19"; break;
261 case WXK_F20
: key
= "F20"; break;
262 case WXK_F21
: key
= "F21"; break;
263 case WXK_F22
: key
= "F22"; break;
264 case WXK_F23
: key
= "F23"; break;
265 case WXK_F24
: key
= "F24"; break;
266 case WXK_NUMLOCK
: key
= "NUMLOCK"; break;
267 case WXK_SCROLL
: key
= "SCROLL"; break;
268 case WXK_PAGEUP
: key
= "PAGEUP"; break;
269 case WXK_PAGEDOWN
: key
= "PAGEDOWN"; break;
270 case WXK_NUMPAD_SPACE
: key
= "NUMPAD_SPACE"; break;
271 case WXK_NUMPAD_TAB
: key
= "NUMPAD_TAB"; break;
272 case WXK_NUMPAD_ENTER
: key
= "NUMPAD_ENTER"; break;
273 case WXK_NUMPAD_F1
: key
= "NUMPAD_F1"; break;
274 case WXK_NUMPAD_F2
: key
= "NUMPAD_F2"; break;
275 case WXK_NUMPAD_F3
: key
= "NUMPAD_F3"; break;
276 case WXK_NUMPAD_F4
: key
= "NUMPAD_F4"; break;
277 case WXK_NUMPAD_HOME
: key
= "NUMPAD_HOME"; break;
278 case WXK_NUMPAD_LEFT
: key
= "NUMPAD_LEFT"; break;
279 case WXK_NUMPAD_UP
: key
= "NUMPAD_UP"; break;
280 case WXK_NUMPAD_RIGHT
: key
= "NUMPAD_RIGHT"; break;
281 case WXK_NUMPAD_DOWN
: key
= "NUMPAD_DOWN"; break;
282 case WXK_NUMPAD_PRIOR
: key
= "NUMPAD_PRIOR"; break;
283 case WXK_NUMPAD_PAGEUP
: key
= "NUMPAD_PAGEUP"; break;
284 case WXK_NUMPAD_PAGEDOWN
: key
= "NUMPAD_PAGEDOWN"; break;
285 case WXK_NUMPAD_END
: key
= "NUMPAD_END"; break;
286 case WXK_NUMPAD_BEGIN
: key
= "NUMPAD_BEGIN"; break;
287 case WXK_NUMPAD_INSERT
: key
= "NUMPAD_INSERT"; break;
288 case WXK_NUMPAD_DELETE
: key
= "NUMPAD_DELETE"; break;
289 case WXK_NUMPAD_EQUAL
: key
= "NUMPAD_EQUAL"; break;
290 case WXK_NUMPAD_MULTIPLY
: key
= "NUMPAD_MULTIPLY"; break;
291 case WXK_NUMPAD_ADD
: key
= "NUMPAD_ADD"; break;
292 case WXK_NUMPAD_SEPARATOR
: key
= "NUMPAD_SEPARATOR"; break;
293 case WXK_NUMPAD_SUBTRACT
: key
= "NUMPAD_SUBTRACT"; break;
294 case WXK_NUMPAD_DECIMAL
: key
= "NUMPAD_DECIMAL"; break;
297 key
.Printf( _T("unknown (%ld)"), keycode
);
301 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
304 GetChar( event
.ControlDown(), _T('C') ),
305 GetChar( event
.AltDown(), _T('A') ),
306 GetChar( event
.ShiftDown(), _T('S') ),
307 GetChar( event
.MetaDown(), _T('M') ) );
311 void MyTextCtrl::OnChar(wxKeyEvent
& event
)
313 LogEvent( _T("Char"), event
);
318 void MyTextCtrl::OnKeyUp(wxKeyEvent
& event
)
320 LogEvent( _("Key up"), event
);
325 void MyTextCtrl::OnKeyDown(wxKeyEvent
& event
)
327 switch ( event
.KeyCode() )
330 // show current position and text length
332 long line
, column
, pos
= GetInsertionPoint();
333 PositionToXY(pos
, &column
, &line
);
335 wxLogMessage( _T("Current position: %ld\n"
336 "Current line, column: (%ld, %ld)\n"
337 "Number of lines: %ld\n"
338 "Current line length: %ld\n"
339 "Total text length: %ld"),
350 SetInsertionPointEnd();
355 SetInsertionPoint(10);
359 LogEvent( _("Key down"), event
);
364 //----------------------------------------------------------------------
366 //----------------------------------------------------------------------
368 const int ID_TEXT
= 150;
369 const int ID_PASTE_TEXT
= 151;
370 const int ID_COPY_TEXT
= 152;
371 const int ID_MOVE_END_ENTRY
= 153;
372 const int ID_MOVE_END_ZONE
= 154;
374 BEGIN_EVENT_TABLE(MyPanel
, wxPanel
)
375 EVT_BUTTON (ID_PASTE_TEXT
, MyPanel::OnPasteFromClipboard
)
376 EVT_BUTTON (ID_COPY_TEXT
, MyPanel::OnCopyToClipboard
)
377 EVT_BUTTON (ID_MOVE_END_ZONE
, MyPanel::OnMoveToEndOfText
)
378 EVT_BUTTON (ID_MOVE_END_ENTRY
, MyPanel::OnMoveToEndOfEntry
)
381 MyPanel::MyPanel( wxFrame
*frame
, int x
, int y
, int w
, int h
)
382 : wxPanel( frame
, -1, wxPoint(x
, y
), wxSize(w
, h
) )
384 m_log
= new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE
);
386 delete wxLog::SetActiveTarget(new wxLogStderr
);
388 // single line text controls
390 m_text
= new MyTextCtrl( this, -1, "Single line.",
391 wxPoint(10,10), wxSize(140,-1), 0);
392 (*m_text
) << " Appended.";
393 m_text
->SetInsertionPoint(0);
394 m_text
->WriteText( "Prepended. " );
396 m_password
= new MyTextCtrl( this, -1, "",
397 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD
);
399 m_readonly
= new MyTextCtrl( this, -1, "Read only",
400 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY
);
402 // multi line text controls
404 m_horizontal
= new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
405 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE
| wxHSCROLL
);
406 m_horizontal
->SetFont(wxFont(18, wxSWISS
, wxNORMAL
, wxBOLD
));
408 m_multitext
= new MyTextCtrl( this, ID_TEXT
, "Multi line.",
409 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE
);
410 (*m_multitext
) << " Appended.";
411 m_multitext
->SetInsertionPoint(0);
412 m_multitext
->WriteText( "Prepended. " );
415 m_multitext
->SetToolTip("Press F1 here.");
418 m_tab
= new MyTextCtrl( this, -1, "Multiline, allow <TAB> processing.",
419 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_TAB
);
421 m_enter
= new MyTextCtrl( this, -1, "Multiline, allow <ENTER> processing.",
422 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE
| wxTE_PROCESS_ENTER
);
426 (void)new wxStaticBox( this, -1, "&Move cursor to the end of:", wxPoint(445, 10), wxSize(160, 100) );
427 button
= new wxButton( this, ID_MOVE_END_ENTRY
, "&Single-line", wxPoint(470, 30), wxSize(110, 30) );
429 button
->SetToolTip("Move cursor in single-line text control to end of line.");
431 button
= new wxButton( this, ID_MOVE_END_ZONE
, "&Multi-line", wxPoint(470, 70), wxSize(110, 30) );
433 button
->SetToolTip("Move cursor in multi-line text control to end of first line.");
436 (void)new wxStaticBox( this, -1, "wx&Clipboard", wxPoint(445,130), wxSize(160,100) );
437 button
= new wxButton( this, ID_COPY_TEXT
, "C&opy line 1", wxPoint(470,150), wxSize(110,30) );
439 button
->SetToolTip("Copy first line of the multi-line text control to the clipboard");
441 button
= new wxButton( this, ID_PASTE_TEXT
, "&Paste text", wxPoint(470,190), wxSize(110,30) );
443 button
->SetToolTip("Paste text from clipboard to the end of the multi-line text control.");
447 void MyPanel::OnPasteFromClipboard( wxCommandEvent
&WXUNUSED(event
) )
449 // We test for wxUSE_DRAG_AND_DROP also, because data objects
450 // may not be implemented for compilers that can't cope with the OLE
451 // parts in wxUSE_DRAG_AND_DROP.
453 #if wxUSE_CLIPBOARD && wxUSE_DRAG_AND_DROP
455 // On X11, we want to get the data from the primary selection instead
456 // of the normal clipboard (which isn't normal under X11 at all). This
457 // call has no effect under MSW.
458 wxTheClipboard
->UsePrimarySelection();
460 if (!wxTheClipboard
->Open())
462 *m_log
<< "Error opening the clipboard.\n";
467 *m_log
<< "Successfully opened the clipboard.\n";
470 wxTextDataObject data
;
472 if (wxTheClipboard
->IsSupported( data
.GetFormat() ))
474 *m_log
<< "Clipboard supports requested format.\n";
476 if (wxTheClipboard
->GetData( &data
))
478 *m_log
<< "Successfully retrieved data from the clipboard.\n";
479 *m_multitext
<< data
.GetText() << "\n";
483 *m_log
<< "Error getting data from the clipboard.\n";
488 *m_log
<< "Clipboard doesn't support requested format.\n";
491 wxTheClipboard
->Close();
493 *m_log
<< "Closed the clipboard.\n";
495 wxLogError("Your version of wxWindows is compiled without clipboard support.");
499 void MyPanel::OnCopyToClipboard( wxCommandEvent
&WXUNUSED(event
) )
501 #if wxUSE_CLIPBOARD && wxUSE_DRAG_AND_DROP
502 wxString
text( m_multitext
->GetLineText(0) );
506 *m_log
<< "No text to copy.\n";
511 if (!wxTheClipboard
->Open())
513 *m_log
<< "Error opening the clipboard.\n";
519 *m_log
<< "Successfully opened the clipboard.\n";
522 wxTextDataObject
*data
= new wxTextDataObject( text
);
524 if (!wxTheClipboard
->SetData( data
))
526 *m_log
<< "Error while copying to the clipboard.\n";
530 *m_log
<< "Successfully copied data to the clipboard.\n";
533 wxTheClipboard
->Close();
535 *m_log
<< "Closed the clipboard.\n";
537 wxLogError("Your version of wxWindows is compiled without clipboard support.");
541 void MyPanel::OnMoveToEndOfText( wxCommandEvent
&event
)
543 m_multitext
->SetInsertionPointEnd();
544 m_multitext
->SetFocus();
547 void MyPanel::OnMoveToEndOfEntry( wxCommandEvent
&event
)
549 m_text
->SetInsertionPointEnd();
553 //----------------------------------------------------------------------
555 //----------------------------------------------------------------------
557 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
558 EVT_MENU(MINIMAL_QUIT
, MyFrame::OnQuit
)
559 EVT_MENU(MINIMAL_ABOUT
, MyFrame::OnAbout
)
561 EVT_MENU(MINIMAL_SET_TOOLTIP_DELAY
, MyFrame::OnSetTooltipDelay
)
562 EVT_MENU(MINIMAL_ENABLE_TOOLTIPS
, MyFrame::OnToggleTooltips
)
563 #endif // wxUSE_TOOLTIPS
564 EVT_IDLE(MyFrame::OnIdle
)
567 MyFrame::MyFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
)
568 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
), wxCAPTION
)
572 (void)new MyPanel( this, 10, 10, 300, 100 );
575 void MyFrame::OnQuit (wxCommandEvent
& WXUNUSED(event
) )
580 void MyFrame::OnAbout( wxCommandEvent
& WXUNUSED(event
) )
584 wxMessageDialog
dialog(this, "This is a text control sample. It demonstrates the many different text control\n"
585 "styles, the use of the clipboard, setting and handling tooltips and intercepting\n"
586 "key and char events.\n"
588 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
589 "About Text Controls", wxOK
);
596 void MyFrame::OnSetTooltipDelay(wxCommandEvent
& event
)
598 static long s_delay
= 5000;
601 delay
.Printf( _T("%ld"), s_delay
);
603 delay
= wxGetTextFromUser("Enter delay (in milliseconds)",
610 wxSscanf(delay
, _T("%ld"), &s_delay
);
612 wxToolTip::SetDelay(s_delay
);
614 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay
);
617 void MyFrame::OnToggleTooltips(wxCommandEvent
& event
)
619 static bool s_enabled
= TRUE
;
621 s_enabled
= !s_enabled
;
623 wxToolTip::Enable(s_enabled
);
625 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled
? _T("en") : _T("dis") );
629 void MyFrame::OnIdle( wxIdleEvent
& event
)
631 // track the window which has the focus in the status bar
632 static wxWindow
*s_windowFocus
= (wxWindow
*)NULL
;
633 wxWindow
*focus
= wxWindow::FindFocus();
634 if ( focus
&& (focus
!= s_windowFocus
) )
636 s_windowFocus
= focus
;
641 _T("Focus: wxWindow = %p, HWND = %08x"),
643 _T("Focus: wxWindow = %p"),
647 , s_windowFocus
->GetHWND()