]> git.saurik.com Git - wxWidgets.git/blob - samples/text/text.cpp
d592388bf4480386dd284a16f62b7e6e5b9bdf0a
[wxWidgets.git] / samples / text / text.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 // 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
41 #endif
42
43 //----------------------------------------------------------------------
44 // class definitions
45 //----------------------------------------------------------------------
46
47 class MyApp: public wxApp
48 {
49 public:
50 bool OnInit();
51 };
52
53 // a text ctrl which allows to call different wxTextCtrl functions
54 // interactively by pressing function keys in it
55 class MyTextCtrl : public wxTextCtrl
56 {
57 public:
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; }
61
62 void OnKeyDown(wxKeyEvent& event);
63 void OnKeyUp(wxKeyEvent& event);
64 void OnChar(wxKeyEvent& event);
65
66 bool m_hasCapture;
67
68 private:
69 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
70 void LogEvent(const wxChar *name, wxKeyEvent& event) const;
71
72 DECLARE_EVENT_TABLE()
73 };
74
75 class MyPanel: public wxPanel
76 {
77 public:
78 MyPanel(wxFrame *frame, int x, int y, int w, int h);
79
80 #if wxUSE_CLIPBOARD
81 void DoPasteFromClipboard();
82 void DoCopyToClipboard();
83 #endif // wxUSE_CLIPBOARD
84
85 void DoMoveToEndOfText();
86 void DoMoveToEndOfEntry();
87
88 void OnSize( wxSizeEvent &event );
89
90 MyTextCtrl *m_text;
91 MyTextCtrl *m_password;
92 MyTextCtrl *m_enter;
93 MyTextCtrl *m_tab;
94 MyTextCtrl *m_readonly;
95
96 MyTextCtrl *m_multitext;
97 MyTextCtrl *m_horizontal;
98
99 wxTextCtrl *m_log;
100
101 private:
102 DECLARE_EVENT_TABLE()
103 };
104
105 class MyFrame: public wxFrame
106 {
107 public:
108 MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h);
109
110 void OnQuit(wxCommandEvent& event);
111 void OnAbout(wxCommandEvent& event);
112 #if wxUSE_TOOLTIPS
113 void OnSetTooltipDelay(wxCommandEvent& event);
114 void OnToggleTooltips(wxCommandEvent& event);
115 #endif // wxUSE_TOOLTIPS
116
117 #if wxUSE_CLIPBOARD
118 void OnPasteFromClipboard( wxCommandEvent &event )
119 { m_panel->DoPasteFromClipboard(); }
120 void OnCopyToClipboard( wxCommandEvent &event )
121 { m_panel->DoCopyToClipboard(); }
122 #endif // wxUSE_CLIPBOARD
123
124 void OnMoveToEndOfText( wxCommandEvent &event )
125 { m_panel->DoMoveToEndOfText(); }
126 void OnMoveToEndOfEntry( wxCommandEvent &event )
127 { m_panel->DoMoveToEndOfEntry(); }
128
129 void OnLogClear(wxCommandEvent& event);
130 void OnFileLoad(wxCommandEvent& event);
131
132 void OnIdle( wxIdleEvent& event );
133
134 private:
135 MyPanel *m_panel;
136
137 DECLARE_EVENT_TABLE()
138 };
139
140 //----------------------------------------------------------------------
141 // main()
142 //----------------------------------------------------------------------
143
144 IMPLEMENT_APP(MyApp)
145
146 //----------------------------------------------------------------------
147 // MyApp
148 //----------------------------------------------------------------------
149
150 enum
151 {
152 TEXT_QUIT = 100,
153 TEXT_ABOUT,
154 TEXT_LOAD,
155 TEXT_CLEAR,
156
157 // clipboard menu
158 TEXT_CLIPBOARD_COPY = 200,
159 TEXT_CLIPBOARD_PASTE,
160
161 // tooltip menu
162 TEXT_TOOLTIPS_SETDELAY = 300,
163 TEXT_TOOLTIPS_ENABLE,
164
165 // move menu
166 TEXT_MOVE_ENDTEXT = 400,
167 TEXT_MOVE_ENDENTRY
168 };
169
170 bool MyApp::OnInit()
171 {
172 // Create the main frame window
173 MyFrame *frame = new MyFrame((wxFrame *) NULL,
174 "Text wxWindows sample", 50, 50, 640, 420);
175 frame->SetSizeHints( 500, 400 );
176
177 wxMenu *file_menu = new wxMenu;
178 file_menu->Append(TEXT_CLEAR, "&Clear the log\tCtrl-C",
179 "Clear the log window contents");
180 file_menu->Append(TEXT_LOAD, "&Load file\tCtrl-O",
181 "Load the sample file into text control");
182 file_menu->AppendSeparator();
183 file_menu->Append(TEXT_ABOUT, "&About\tAlt-A");
184 file_menu->AppendSeparator();
185 file_menu->Append(TEXT_QUIT, "E&xit\tAlt-X", "Quit controls sample");
186
187 wxMenuBar *menu_bar = new wxMenuBar( wxMB_DOCKABLE );
188 menu_bar->Append(file_menu, "&File");
189
190 #if wxUSE_TOOLTIPS
191 wxMenu *tooltip_menu = new wxMenu;
192 tooltip_menu->Append(TEXT_TOOLTIPS_SETDELAY, "Set &delay\tCtrl-D");
193 tooltip_menu->AppendSeparator();
194 tooltip_menu->Append(TEXT_TOOLTIPS_ENABLE, "&Toggle tooltips\tCtrl-T",
195 "enable/disable tooltips", TRUE);
196 tooltip_menu->Check(TEXT_TOOLTIPS_ENABLE, TRUE);
197 menu_bar->Append(tooltip_menu, "&Tooltips");
198 #endif // wxUSE_TOOLTIPS
199
200 #if wxUSE_CLIPBOARD
201 wxMenu *menuClipboard = new wxMenu;
202 menuClipboard->Append(TEXT_CLIPBOARD_COPY, "&Copy\tCtrl-C",
203 "Copy the first line to the clipboard");
204 menuClipboard->Append(TEXT_CLIPBOARD_PASTE, "&Paste\tCtrl-V",
205 "Paste from clipboard to the text control");
206 menu_bar->Append(menuClipboard, "&Clipboard");
207 #endif // wxUSE_CLIPBOARD
208
209 wxMenu *menuMove = new wxMenu;
210 menuMove->Append(TEXT_MOVE_ENDTEXT, "To the end of &text");
211 menuMove->Append(TEXT_MOVE_ENDENTRY, "To the end of &entry");
212 menu_bar->Append(menuMove, "&Move");
213
214 frame->SetMenuBar(menu_bar);
215
216 frame->Show(TRUE);
217
218 SetTopWindow(frame);
219
220 // report success
221 return TRUE;
222 }
223
224 //----------------------------------------------------------------------
225 // MyTextCtrl
226 //----------------------------------------------------------------------
227
228 BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
229 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
230 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
231 EVT_CHAR(MyTextCtrl::OnChar)
232 END_EVENT_TABLE()
233
234 void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const
235 {
236 wxString key;
237 long keycode = event.KeyCode();
238 {
239 switch ( keycode )
240 {
241 case WXK_BACK: key = "BACK"; break;
242 case WXK_TAB: key = "TAB"; break;
243 case WXK_RETURN: key = "RETURN"; break;
244 case WXK_ESCAPE: key = "ESCAPE"; break;
245 case WXK_SPACE: key = "SPACE"; break;
246 case WXK_DELETE: key = "DELETE"; break;
247 case WXK_START: key = "START"; break;
248 case WXK_LBUTTON: key = "LBUTTON"; break;
249 case WXK_RBUTTON: key = "RBUTTON"; break;
250 case WXK_CANCEL: key = "CANCEL"; break;
251 case WXK_MBUTTON: key = "MBUTTON"; break;
252 case WXK_CLEAR: key = "CLEAR"; break;
253 case WXK_SHIFT: key = "SHIFT"; break;
254 case WXK_ALT: key = "ALT"; break;
255 case WXK_CONTROL: key = "CONTROL"; break;
256 case WXK_MENU: key = "MENU"; break;
257 case WXK_PAUSE: key = "PAUSE"; break;
258 case WXK_CAPITAL: key = "CAPITAL"; break;
259 case WXK_PRIOR: key = "PRIOR"; break;
260 case WXK_NEXT: key = "NEXT"; break;
261 case WXK_END: key = "END"; break;
262 case WXK_HOME: key = "HOME"; break;
263 case WXK_LEFT: key = "LEFT"; break;
264 case WXK_UP: key = "UP"; break;
265 case WXK_RIGHT: key = "RIGHT"; break;
266 case WXK_DOWN: key = "DOWN"; break;
267 case WXK_SELECT: key = "SELECT"; break;
268 case WXK_PRINT: key = "PRINT"; break;
269 case WXK_EXECUTE: key = "EXECUTE"; break;
270 case WXK_SNAPSHOT: key = "SNAPSHOT"; break;
271 case WXK_INSERT: key = "INSERT"; break;
272 case WXK_HELP: key = "HELP"; break;
273 case WXK_NUMPAD0: key = "NUMPAD0"; break;
274 case WXK_NUMPAD1: key = "NUMPAD1"; break;
275 case WXK_NUMPAD2: key = "NUMPAD2"; break;
276 case WXK_NUMPAD3: key = "NUMPAD3"; break;
277 case WXK_NUMPAD4: key = "NUMPAD4"; break;
278 case WXK_NUMPAD5: key = "NUMPAD5"; break;
279 case WXK_NUMPAD6: key = "NUMPAD6"; break;
280 case WXK_NUMPAD7: key = "NUMPAD7"; break;
281 case WXK_NUMPAD8: key = "NUMPAD8"; break;
282 case WXK_NUMPAD9: key = "NUMPAD9"; break;
283 case WXK_MULTIPLY: key = "MULTIPLY"; break;
284 case WXK_ADD: key = "ADD"; break;
285 case WXK_SEPARATOR: key = "SEPARATOR"; break;
286 case WXK_SUBTRACT: key = "SUBTRACT"; break;
287 case WXK_DECIMAL: key = "DECIMAL"; break;
288 case WXK_DIVIDE: key = "DIVIDE"; break;
289 case WXK_F1: key = "F1"; break;
290 case WXK_F2: key = "F2"; break;
291 case WXK_F3: key = "F3"; break;
292 case WXK_F4: key = "F4"; break;
293 case WXK_F5: key = "F5"; break;
294 case WXK_F6: key = "F6"; break;
295 case WXK_F7: key = "F7"; break;
296 case WXK_F8: key = "F8"; break;
297 case WXK_F9: key = "F9"; break;
298 case WXK_F10: key = "F10"; break;
299 case WXK_F11: key = "F11"; break;
300 case WXK_F12: key = "F12"; break;
301 case WXK_F13: key = "F13"; break;
302 case WXK_F14: key = "F14"; break;
303 case WXK_F15: key = "F15"; break;
304 case WXK_F16: key = "F16"; break;
305 case WXK_F17: key = "F17"; break;
306 case WXK_F18: key = "F18"; break;
307 case WXK_F19: key = "F19"; break;
308 case WXK_F20: key = "F20"; break;
309 case WXK_F21: key = "F21"; break;
310 case WXK_F22: key = "F22"; break;
311 case WXK_F23: key = "F23"; break;
312 case WXK_F24: key = "F24"; break;
313 case WXK_NUMLOCK: key = "NUMLOCK"; break;
314 case WXK_SCROLL: key = "SCROLL"; break;
315 case WXK_PAGEUP: key = "PAGEUP"; break;
316 case WXK_PAGEDOWN: key = "PAGEDOWN"; break;
317 case WXK_NUMPAD_SPACE: key = "NUMPAD_SPACE"; break;
318 case WXK_NUMPAD_TAB: key = "NUMPAD_TAB"; break;
319 case WXK_NUMPAD_ENTER: key = "NUMPAD_ENTER"; break;
320 case WXK_NUMPAD_F1: key = "NUMPAD_F1"; break;
321 case WXK_NUMPAD_F2: key = "NUMPAD_F2"; break;
322 case WXK_NUMPAD_F3: key = "NUMPAD_F3"; break;
323 case WXK_NUMPAD_F4: key = "NUMPAD_F4"; break;
324 case WXK_NUMPAD_HOME: key = "NUMPAD_HOME"; break;
325 case WXK_NUMPAD_LEFT: key = "NUMPAD_LEFT"; break;
326 case WXK_NUMPAD_UP: key = "NUMPAD_UP"; break;
327 case WXK_NUMPAD_RIGHT: key = "NUMPAD_RIGHT"; break;
328 case WXK_NUMPAD_DOWN: key = "NUMPAD_DOWN"; break;
329 case WXK_NUMPAD_PRIOR: key = "NUMPAD_PRIOR"; break;
330 case WXK_NUMPAD_PAGEUP: key = "NUMPAD_PAGEUP"; break;
331 case WXK_NUMPAD_PAGEDOWN: key = "NUMPAD_PAGEDOWN"; break;
332 case WXK_NUMPAD_END: key = "NUMPAD_END"; break;
333 case WXK_NUMPAD_BEGIN: key = "NUMPAD_BEGIN"; break;
334 case WXK_NUMPAD_INSERT: key = "NUMPAD_INSERT"; break;
335 case WXK_NUMPAD_DELETE: key = "NUMPAD_DELETE"; break;
336 case WXK_NUMPAD_EQUAL: key = "NUMPAD_EQUAL"; break;
337 case WXK_NUMPAD_MULTIPLY: key = "NUMPAD_MULTIPLY"; break;
338 case WXK_NUMPAD_ADD: key = "NUMPAD_ADD"; break;
339 case WXK_NUMPAD_SEPARATOR: key = "NUMPAD_SEPARATOR"; break;
340 case WXK_NUMPAD_SUBTRACT: key = "NUMPAD_SUBTRACT"; break;
341 case WXK_NUMPAD_DECIMAL: key = "NUMPAD_DECIMAL"; break;
342
343 default:
344 {
345 if ( wxIsprint((int)keycode) )
346 key.Printf( _T("'%c'") , (char)keycode);
347 else
348 key.Printf( _T("unknown (%ld)"), keycode);
349 }
350 }
351 }
352
353 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
354 name,
355 key.c_str(),
356 GetChar( event.ControlDown(), _T('C') ),
357 GetChar( event.AltDown(), _T('A') ),
358 GetChar( event.ShiftDown(), _T('S') ),
359 GetChar( event.MetaDown(), _T('M') ) );
360
361 }
362
363 void MyTextCtrl::OnChar(wxKeyEvent& event)
364 {
365 LogEvent( _T("Char"), event);
366
367 /* How are we supposed to test wxTE_PROCESS_TAB with this code?
368
369 if ( event.KeyCode() == WXK_TAB )
370 {
371 WriteText("\t");
372 }
373 else
374 {
375 event.Skip();
376 }
377 */
378 event.Skip();
379 }
380
381 void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
382 {
383 LogEvent( _("Key up"), event);
384
385 event.Skip();
386 }
387
388 void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
389 {
390 switch ( event.KeyCode() )
391 {
392 case WXK_F1:
393 // show current position and text length
394 {
395 long line, column, pos = GetInsertionPoint();
396 PositionToXY(pos, &column, &line);
397
398 wxLogMessage( _T("Current position: %ld\n"
399 "Current line, column: (%ld, %ld)\n"
400 "Number of lines: %ld\n"
401 "Current line length: %ld\n"
402 "Total text length: %ld"),
403 pos,
404 line, column,
405 GetNumberOfLines(),
406 GetLineLength(line),
407 GetLastPosition());
408 }
409 break;
410
411 case WXK_F2:
412 // go to the end
413 SetInsertionPointEnd();
414 break;
415
416 case WXK_F3:
417 // go to position 10
418 SetInsertionPoint(10);
419 break;
420
421 case WXK_F4:
422 if (!m_hasCapture)
423 {
424 wxLogDebug( wxT("Now capturing mouse and events.") );
425 m_hasCapture = TRUE;
426 CaptureMouse();
427 }
428 else
429 {
430 wxLogDebug( wxT("Stopped capturing mouse and events.") );
431 m_hasCapture = TRUE;
432 ReleaseMouse();
433 }
434 break;
435
436 case WXK_F5:
437 // insert a blank line
438 WriteText("\n");
439 }
440
441 LogEvent( _("Key down"), event);
442
443 event.Skip();
444 }
445
446 //----------------------------------------------------------------------
447 // MyPanel
448 //----------------------------------------------------------------------
449
450 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
451 EVT_SIZE(MyPanel::OnSize)
452 END_EVENT_TABLE()
453
454 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
455 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
456 {
457 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE );
458
459 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
460
461 delete old_log;
462
463 // single line text controls
464
465 m_text = new MyTextCtrl( this, -1, "Single line.",
466 wxPoint(10,10), wxSize(140,-1), 0);
467 (*m_text) << " Appended.";
468 m_text->SetInsertionPoint(0);
469 m_text->WriteText( "Prepended. " );
470
471 m_password = new MyTextCtrl( this, -1, "",
472 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD );
473
474 m_readonly = new MyTextCtrl( this, -1, "Read only",
475 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY );
476
477 // multi line text controls
478
479 m_horizontal = new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
480 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL );
481 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
482 FALSE, "", wxFONTENCODING_KOI8));
483
484 m_multitext = new MyTextCtrl( this, -1, "Multi line.",
485 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE );
486 (*m_multitext) << " Appended.";
487 m_multitext->SetInsertionPoint(0);
488 m_multitext->WriteText( "Prepended. " );
489
490 #if wxUSE_TOOLTIPS
491 m_multitext->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
492 #endif
493
494 m_tab = new MyTextCtrl( this, -1, "Multiline, allow <TAB> processing.",
495 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE | wxTE_PROCESS_TAB );
496
497 m_enter = new MyTextCtrl( this, -1, "Multiline, allow <ENTER> processing.",
498 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE);
499 }
500
501 void MyPanel::OnSize( wxSizeEvent &event )
502 {
503 wxSize client_area( GetClientSize() );
504 m_log->SetSize( 0, 260, client_area.x, client_area.y - 260 );
505 event.Skip();
506 }
507
508 #if wxUSE_CLIPBOARD
509 void MyPanel::DoPasteFromClipboard()
510 {
511 // On X11, we want to get the data from the primary selection instead
512 // of the normal clipboard (which isn't normal under X11 at all). This
513 // call has no effect under MSW.
514 wxTheClipboard->UsePrimarySelection();
515
516 if (!wxTheClipboard->Open())
517 {
518 *m_log << "Error opening the clipboard.\n";
519 return;
520 }
521 else
522 {
523 *m_log << "Successfully opened the clipboard.\n";
524 }
525
526 wxTextDataObject data;
527
528 if (wxTheClipboard->IsSupported( data.GetFormat() ))
529 {
530 *m_log << "Clipboard supports requested format.\n";
531
532 if (wxTheClipboard->GetData( data ))
533 {
534 *m_log << "Successfully retrieved data from the clipboard.\n";
535 *m_multitext << data.GetText() << "\n";
536 }
537 else
538 {
539 *m_log << "Error getting data from the clipboard.\n";
540 }
541 }
542 else
543 {
544 *m_log << "Clipboard doesn't support requested format.\n";
545 }
546
547 wxTheClipboard->Close();
548
549 *m_log << "Closed the clipboard.\n";
550 }
551
552 void MyPanel::DoCopyToClipboard()
553 {
554 // On X11, we want to get the data from the primary selection instead
555 // of the normal clipboard (which isn't normal under X11 at all). This
556 // call has no effect under MSW.
557 wxTheClipboard->UsePrimarySelection();
558
559 wxString text( m_multitext->GetLineText(0) );
560
561 if (text.IsEmpty())
562 {
563 *m_log << "No text to copy.\n";
564
565 return;
566 }
567
568 if (!wxTheClipboard->Open())
569 {
570 *m_log << "Error opening the clipboard.\n";
571
572 return;
573 }
574 else
575 {
576 *m_log << "Successfully opened the clipboard.\n";
577 }
578
579 wxTextDataObject *data = new wxTextDataObject( text );
580
581 if (!wxTheClipboard->SetData( data ))
582 {
583 *m_log << "Error while copying to the clipboard.\n";
584 }
585 else
586 {
587 *m_log << "Successfully copied data to the clipboard.\n";
588 }
589
590 wxTheClipboard->Close();
591
592 *m_log << "Closed the clipboard.\n";
593 }
594
595 #endif // wxUSE_CLIPBOARD
596
597 void MyPanel::DoMoveToEndOfText()
598 {
599 m_multitext->SetInsertionPointEnd();
600 m_multitext->SetFocus();
601 }
602
603 void MyPanel::DoMoveToEndOfEntry()
604 {
605 m_text->SetInsertionPointEnd();
606 m_text->SetFocus();
607 }
608
609 //----------------------------------------------------------------------
610 // MyFrame
611 //----------------------------------------------------------------------
612
613 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
614 EVT_MENU(TEXT_QUIT, MyFrame::OnQuit)
615 EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout)
616 EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad)
617 EVT_MENU(TEXT_CLEAR, MyFrame::OnLogClear)
618
619 #if wxUSE_TOOLTIPS
620 EVT_MENU(TEXT_TOOLTIPS_SETDELAY, MyFrame::OnSetTooltipDelay)
621 EVT_MENU(TEXT_TOOLTIPS_ENABLE, MyFrame::OnToggleTooltips)
622 #endif // wxUSE_TOOLTIPS
623
624 #if wxUSE_CLIPBOARD
625 EVT_MENU(TEXT_CLIPBOARD_PASTE, MyFrame::OnPasteFromClipboard)
626 EVT_MENU(TEXT_CLIPBOARD_COPY, MyFrame::OnCopyToClipboard)
627 #endif // wxUSE_CLIPBOARD
628
629 EVT_MENU(TEXT_MOVE_ENDTEXT, MyFrame::OnMoveToEndOfText)
630 EVT_MENU(TEXT_MOVE_ENDENTRY, MyFrame::OnMoveToEndOfEntry)
631
632 EVT_IDLE(MyFrame::OnIdle)
633 END_EVENT_TABLE()
634
635 MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h)
636 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h) )
637 {
638 CreateStatusBar(2);
639
640 m_panel = new MyPanel( this, 10, 10, 300, 100 );
641 }
642
643 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
644 {
645 Close(TRUE);
646 }
647
648 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
649 {
650 wxBeginBusyCursor();
651
652 wxMessageDialog dialog(this,
653 "This is a text control sample. It demonstrates the many different\n"
654 "text control styles, the use of the clipboard, setting and handling\n"
655 "tooltips and intercepting key and char events.\n"
656 "\n"
657 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
658 "About Text Controls",
659 wxOK | wxICON_INFORMATION);
660
661 dialog.ShowModal();
662
663 wxEndBusyCursor();
664 }
665
666 #if wxUSE_TOOLTIPS
667 void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
668 {
669 static long s_delay = 5000;
670
671 wxString delay;
672 delay.Printf( _T("%ld"), s_delay);
673
674 delay = wxGetTextFromUser("Enter delay (in milliseconds)",
675 "Set tooltip delay",
676 delay,
677 this);
678 if ( !delay )
679 return; // cancelled
680
681 wxSscanf(delay, _T("%ld"), &s_delay);
682
683 wxToolTip::SetDelay(s_delay);
684
685 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
686 }
687
688 void MyFrame::OnToggleTooltips(wxCommandEvent& event)
689 {
690 static bool s_enabled = TRUE;
691
692 s_enabled = !s_enabled;
693
694 wxToolTip::Enable(s_enabled);
695
696 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
697 }
698 #endif // tooltips
699
700 void MyFrame::OnLogClear(wxCommandEvent& WXUNUSED(event))
701 {
702 m_panel->m_log->Clear();
703 }
704
705 void MyFrame::OnFileLoad(wxCommandEvent& event)
706 {
707 if ( m_panel->m_multitext->LoadFile("text.rc") )
708 wxLogStatus(this, _T("Successfully loaded file"));
709 else
710 wxLogStatus(this, _T("Couldn't load the file"));
711 }
712
713 void MyFrame::OnIdle( wxIdleEvent& event )
714 {
715 // track the window which has the focus in the status bar
716 static wxWindow *s_windowFocus = (wxWindow *)NULL;
717 wxWindow *focus = wxWindow::FindFocus();
718 if ( focus && (focus != s_windowFocus) )
719 {
720 s_windowFocus = focus;
721
722 wxString msg;
723 msg.Printf(
724 #ifdef __WXMSW__
725 _T("Focus: wxWindow = %p, HWND = %08x"),
726 #else
727 _T("Focus: wxWindow = %p"),
728 #endif
729 s_windowFocus
730 #ifdef __WXMSW__
731 , s_windowFocus->GetHWND()
732 #endif
733 );
734
735 SetStatusText(msg);
736 }
737 event.Skip();
738 }