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