]> git.saurik.com Git - wxWidgets.git/blob - samples/text/controls.cpp
Added own sample for text ctrl, char/key events, clipboard.
[wxWidgets.git] / samples / text / controls.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: controls.cpp
3 // Purpose: TextCtrl wxWindows sample
4 // Author: Robert Roebling
5 // Modified by:
6 // RCS-ID: $Id$
7 // Copyright: (c) Robert Roebling, Julian Smart
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifdef __GNUG__
12 #pragma implementation "controls.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #if wxUSE_CLIPBOARD
27 #include "wx/dataobj.h"
28 #include "wx/clipbrd.h"
29 #endif
30
31 #if wxUSE_TOOLTIPS
32 #include "wx/tooltip.h"
33 #endif
34
35 #if defined(__WXGTK__) || defined(__WXMOTIF__)
36 #include "mondrian.xpm"
37 #endif
38
39 //----------------------------------------------------------------------
40 // class definitions
41 //----------------------------------------------------------------------
42
43 class MyApp: public wxApp
44 {
45 public:
46 bool OnInit();
47 };
48
49 // a text ctrl which allows to call different wxTextCtrl functions
50 // interactively by pressing function keys in it
51 class MyTextCtrl : public wxTextCtrl
52 {
53 public:
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) { }
57
58 void OnKeyDown(wxKeyEvent& event);
59 void OnKeyUp(wxKeyEvent& event);
60 void OnChar(wxKeyEvent& event);
61
62 private:
63 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
64 void LogEvent(const wxChar *name, wxKeyEvent& event) const;
65
66 DECLARE_EVENT_TABLE()
67 };
68
69 class MyPanel: public wxPanel
70 {
71 public:
72 MyPanel(wxFrame *frame, int x, int y, int w, int h);
73
74 void OnPasteFromClipboard( wxCommandEvent &event );
75 void OnCopyToClipboard( wxCommandEvent &event );
76 void OnMoveToEndOfText( wxCommandEvent &event );
77 void OnMoveToEndOfEntry( wxCommandEvent &event );
78
79 MyTextCtrl *m_text;
80 MyTextCtrl *m_password;
81 MyTextCtrl *m_enter;
82 MyTextCtrl *m_tab;
83 MyTextCtrl *m_entertab;
84 MyTextCtrl *m_readonly;
85
86 MyTextCtrl *m_multitext;
87 MyTextCtrl *m_horizontal;
88 MyTextCtrl *m_multitab;
89
90 wxTextCtrl *m_log;
91
92 private:
93 DECLARE_EVENT_TABLE()
94 };
95
96 class MyFrame: public wxFrame
97 {
98 public:
99 MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h);
100
101 void OnQuit(wxCommandEvent& event);
102 void OnAbout(wxCommandEvent& event);
103 #if wxUSE_TOOLTIPS
104 void OnSetTooltipDelay(wxCommandEvent& event);
105 void OnToggleTooltips(wxCommandEvent& event);
106 #endif // wxUSE_TOOLTIPS
107 void OnIdle( wxIdleEvent& event );
108
109 private:
110 DECLARE_EVENT_TABLE()
111 };
112
113 //----------------------------------------------------------------------
114 // main()
115 //----------------------------------------------------------------------
116
117 IMPLEMENT_APP(MyApp)
118
119 //----------------------------------------------------------------------
120 // MyApp
121 //----------------------------------------------------------------------
122
123 enum
124 {
125 MINIMAL_QUIT = 100,
126 MINIMAL_TEXT,
127 MINIMAL_ABOUT,
128
129 // tooltip menu
130 MINIMAL_SET_TOOLTIP_DELAY = 200,
131 MINIMAL_ENABLE_TOOLTIPS
132 };
133
134 bool MyApp::OnInit()
135 {
136 // Create the main frame window
137 MyFrame *frame = new MyFrame((wxFrame *) NULL,
138 "Text wxWindows App",
139 50, 50, 640, 420);
140
141 // Give it an icon
142 // The wxICON() macros loads an icon from a resource under Windows
143 // and uses an #included XPM image under GTK+ and Motif
144
145 frame->SetIcon( wxICON(mondrian) );
146
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");
150
151 wxMenuBar *menu_bar = new wxMenuBar( wxMB_DOCKABLE );
152 menu_bar->Append(file_menu, "&File");
153
154 #if wxUSE_TOOLTIPS
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
163
164 frame->SetMenuBar(menu_bar);
165
166 frame->Show(TRUE);
167
168 SetTopWindow(frame);
169
170 // report success
171 return TRUE;
172 }
173
174 //----------------------------------------------------------------------
175 // MyTextCtrl
176 //----------------------------------------------------------------------
177
178 BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
179 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
180 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
181 EVT_CHAR(MyTextCtrl::OnChar)
182 END_EVENT_TABLE()
183
184 void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const
185 {
186 wxString key;
187 long keycode = event.KeyCode();
188 if ( wxIsprint((int)keycode) )
189 key.Printf( _T("'%c'") , (char)keycode);
190 else
191 {
192 switch ( keycode )
193 {
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;
295
296 default:
297 key.Printf( _T("unknown (%ld)"), keycode);
298 }
299 }
300
301 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
302 name,
303 key.c_str(),
304 GetChar( event.ControlDown(), _T('C') ),
305 GetChar( event.AltDown(), _T('A') ),
306 GetChar( event.ShiftDown(), _T('S') ),
307 GetChar( event.MetaDown(), _T('M') ) );
308
309 }
310
311 void MyTextCtrl::OnChar(wxKeyEvent& event)
312 {
313 LogEvent( _T("Char"), event);
314
315 event.Skip();
316 }
317
318 void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
319 {
320 LogEvent( _("Key up"), event);
321
322 event.Skip();
323 }
324
325 void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
326 {
327 switch ( event.KeyCode() )
328 {
329 case WXK_F1:
330 // show current position and text length
331 {
332 long line, column, pos = GetInsertionPoint();
333 PositionToXY(pos, &column, &line);
334
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"),
340 pos,
341 line, column,
342 GetNumberOfLines(),
343 GetLineLength(line),
344 GetLastPosition());
345 }
346 break;
347
348 case WXK_F2:
349 // go to the end
350 SetInsertionPointEnd();
351 break;
352
353 case WXK_F3:
354 // go to position 10
355 SetInsertionPoint(10);
356 break;
357 }
358
359 LogEvent( _("Key down"), event);
360
361 event.Skip();
362 }
363
364 //----------------------------------------------------------------------
365 // MyPanel
366 //----------------------------------------------------------------------
367
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;
373
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)
379 END_EVENT_TABLE()
380
381 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
382 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
383 {
384 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE );
385
386 delete wxLog::SetActiveTarget(new wxLogStderr);
387
388 // single line text controls
389
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. " );
395
396 m_password = new MyTextCtrl( this, -1, "",
397 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD );
398
399 m_readonly = new MyTextCtrl( this, -1, "Read only",
400 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY );
401
402 // multi line text controls
403
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
407 m_multitext = new MyTextCtrl( this, ID_TEXT, "Multi line.",
408 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE );
409 (*m_multitext) << " Appended.";
410 m_multitext->SetInsertionPoint(0);
411 m_multitext->WriteText( "Prepended. " );
412
413 #if wxUSE_TOOLTIPS
414 m_multitext->SetToolTip("Press F1 here.");
415 #endif
416
417 m_tab = new MyTextCtrl( this, -1, "Multiline, allow <TAB> processing.",
418 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE | wxTE_PROCESS_TAB );
419
420 m_enter = new MyTextCtrl( this, -1, "Multiline, allow <ENTER> processing.",
421 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE | wxTE_PROCESS_ENTER );
422
423 wxButton *button;
424
425 (void)new wxStaticBox( this, -1, "&Move cursor to the end of:", wxPoint(445, 10), wxSize(160, 100) );
426 button = new wxButton( this, ID_MOVE_END_ENTRY, "&Single-line", wxPoint(470, 30), wxSize(110, 30) );
427 #if wxUSE_TOOLTIPS
428 button->SetToolTip("Move cursor in single-line text control to end of line.");
429 #endif
430 button = new wxButton( this, ID_MOVE_END_ZONE, "&Multi-line", wxPoint(470, 70), wxSize(110, 30) );
431 #if wxUSE_TOOLTIPS
432 button->SetToolTip("Move cursor in multi-line text control to end of first line.");
433 #endif
434
435 (void)new wxStaticBox( this, -1, "wx&Clipboard", wxPoint(445,130), wxSize(160,100) );
436 button = new wxButton( this, ID_COPY_TEXT, "C&opy line 1", wxPoint(470,150), wxSize(110,30) );
437 #if wxUSE_TOOLTIPS
438 button->SetToolTip("Copy first line of the multi-line text control to the clipboard");
439 #endif
440 button = new wxButton( this, ID_PASTE_TEXT, "&Paste text", wxPoint(470,190), wxSize(110,30) );
441 #if wxUSE_TOOLTIPS
442 button->SetToolTip("Paste text from clipboard to the end of the multi-line text control.");
443 #endif
444 }
445
446 void MyPanel::OnPasteFromClipboard( wxCommandEvent &WXUNUSED(event) )
447 {
448 // We test for wxUSE_DRAG_AND_DROP also, because data objects
449 // may not be implemented for compilers that can't cope with the OLE
450 // parts in wxUSE_DRAG_AND_DROP.
451
452 #if wxUSE_CLIPBOARD && wxUSE_DRAG_AND_DROP
453 if (!wxTheClipboard->Open())
454 {
455 *m_log << "Error opening the clipboard.\n";
456
457 return;
458 }
459 else
460 {
461 *m_log << "Successfully opened the clipboard.\n";
462 }
463
464 wxTextDataObject data;
465
466 if (wxTheClipboard->IsSupported( data.GetFormat() ))
467 {
468 *m_log << "Clipboard supports requested format.\n";
469
470 if (wxTheClipboard->GetData( &data ))
471 {
472 *m_log << "Successfully retrieved data from the clipboard.\n";
473 *m_multitext << data.GetText() << "\n";
474 }
475 else
476 {
477 *m_log << "Error getting data from the clipboard.\n";
478 }
479 }
480 else
481 {
482 *m_log << "Clipboard doesn't support requested format.\n";
483 }
484
485 wxTheClipboard->Close();
486
487 *m_log << "Closed the clipboard.\n";
488 #else
489 wxLogError("Your version of wxWindows is compiled without clipboard support.");
490 #endif
491 }
492
493 void MyPanel::OnCopyToClipboard( wxCommandEvent &WXUNUSED(event) )
494 {
495 #if wxUSE_CLIPBOARD && wxUSE_DRAG_AND_DROP
496 wxString text( m_multitext->GetLineText(0) );
497
498 if (text.IsEmpty())
499 {
500 *m_log << "No text to copy.\n";
501
502 return;
503 }
504
505 if (!wxTheClipboard->Open())
506 {
507 *m_log << "Error opening the clipboard.\n";
508
509 return;
510 }
511 else
512 {
513 *m_log << "Successfully opened the clipboard.\n";
514 }
515
516 wxTextDataObject *data = new wxTextDataObject( text );
517
518 if (!wxTheClipboard->SetData( data ))
519 {
520 *m_log << "Error while copying to the clipboard.\n";
521 }
522 else
523 {
524 *m_log << "Successfully copied data to the clipboard.\n";
525 }
526
527 wxTheClipboard->Close();
528
529 *m_log << "Closed the clipboard.\n";
530 #else
531 wxLogError("Your version of wxWindows is compiled without clipboard support.");
532 #endif
533 }
534
535 void MyPanel::OnMoveToEndOfText( wxCommandEvent &event )
536 {
537 m_multitext->SetInsertionPointEnd();
538 m_multitext->SetFocus();
539 }
540
541 void MyPanel::OnMoveToEndOfEntry( wxCommandEvent &event )
542 {
543 m_text->SetInsertionPointEnd();
544 m_text->SetFocus();
545 }
546
547 //----------------------------------------------------------------------
548 // MyFrame
549 //----------------------------------------------------------------------
550
551 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
552 EVT_MENU(MINIMAL_QUIT, MyFrame::OnQuit)
553 EVT_MENU(MINIMAL_ABOUT, MyFrame::OnAbout)
554 #if wxUSE_TOOLTIPS
555 EVT_MENU(MINIMAL_SET_TOOLTIP_DELAY, MyFrame::OnSetTooltipDelay)
556 EVT_MENU(MINIMAL_ENABLE_TOOLTIPS, MyFrame::OnToggleTooltips)
557 #endif // wxUSE_TOOLTIPS
558 EVT_IDLE(MyFrame::OnIdle)
559 END_EVENT_TABLE()
560
561 MyFrame::MyFrame(wxFrame *frame, char *title, int x, int y, int w, int h)
562 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h), wxCAPTION )
563 {
564 CreateStatusBar(2);
565
566 (void)new MyPanel( this, 10, 10, 300, 100 );
567 }
568
569 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
570 {
571 Close(TRUE);
572 }
573
574 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
575 {
576 wxBeginBusyCursor();
577
578 wxMessageDialog dialog(this, "This is a text control sample. It demonstrates the many different text control\n"
579 "styles, the use of the clipboard, setting and handling tooltips and intercepting\n"
580 "key and char events.\n"
581 "\n"
582 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
583 "About Text Controls", wxOK );
584 dialog.ShowModal();
585
586 wxEndBusyCursor();
587 }
588
589 #if wxUSE_TOOLTIPS
590 void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
591 {
592 static long s_delay = 5000;
593
594 wxString delay;
595 delay.Printf( _T("%ld"), s_delay);
596
597 delay = wxGetTextFromUser("Enter delay (in milliseconds)",
598 "Set tooltip delay",
599 delay,
600 this);
601 if ( !delay )
602 return; // cancelled
603
604 wxSscanf(delay, _T("%ld"), &s_delay);
605
606 wxToolTip::SetDelay(s_delay);
607
608 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
609 }
610
611 void MyFrame::OnToggleTooltips(wxCommandEvent& event)
612 {
613 static bool s_enabled = TRUE;
614
615 s_enabled = !s_enabled;
616
617 wxToolTip::Enable(s_enabled);
618
619 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
620 }
621 #endif // tooltips
622
623 void MyFrame::OnIdle( wxIdleEvent& event )
624 {
625 // track the window which has the focus in the status bar
626 static wxWindow *s_windowFocus = (wxWindow *)NULL;
627 wxWindow *focus = wxWindow::FindFocus();
628 if ( focus && (focus != s_windowFocus) )
629 {
630 s_windowFocus = focus;
631
632 wxString msg;
633 msg.Printf(
634 #ifdef __WXMSW__
635 _T("Focus: wxWindow = %p, HWND = %08x"),
636 #else
637 _T("Focus: wxWindow = %p"),
638 #endif
639 s_windowFocus
640 #ifdef __WXMSW__
641 , s_windowFocus->GetHWND()
642 #endif
643 );
644
645 SetStatusText(msg);
646 }
647 event.Skip();
648 }