]> git.saurik.com Git - wxWidgets.git/blob - samples/text/text.cpp
6fc3a71448255cbd9f123b7bc1aa8fd4523df60f
[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_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
51 class MyApp: public wxApp
52 {
53 public:
54 bool OnInit();
55 };
56
57 // a text ctrl which allows to call different wxTextCtrl functions
58 // interactively by pressing function keys in it
59 class MyTextCtrl : public wxTextCtrl
60 {
61 public:
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
72 private:
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
79 class MyPanel: public wxPanel
80 {
81 public:
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
107 private:
108 DECLARE_EVENT_TABLE()
109 };
110
111 class MyFrame: public wxFrame
112 {
113 public:
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
141 private:
142 MyPanel *m_panel;
143
144 DECLARE_EVENT_TABLE()
145 };
146
147 //----------------------------------------------------------------------
148 // main()
149 //----------------------------------------------------------------------
150
151 IMPLEMENT_APP(MyApp)
152
153 //----------------------------------------------------------------------
154 // MyApp
155 //----------------------------------------------------------------------
156
157 enum
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
178 bool 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
238 BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
239 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
240 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
241 EVT_CHAR(MyTextCtrl::OnChar)
242 END_EVENT_TABLE()
243
244 void 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
373 void MyTextCtrl::OnChar(wxKeyEvent& event)
374 {
375 LogEvent( _T("Char"), event);
376
377 wxWindow *win = (wxWindow *)event.GetEventObject();
378 const wxChar *data = (const wxChar *)(win->GetClientData());
379 if ( data )
380 {
381 wxLogMessage(_T(" (from control '%s')"), data);
382 }
383
384 /* How are we supposed to test wxTE_PROCESS_TAB with this code?
385
386 if ( event.KeyCode() == WXK_TAB )
387 {
388 WriteText("\t");
389 }
390 else
391 {
392 event.Skip();
393 }
394 */
395 event.Skip();
396 }
397
398 void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
399 {
400 LogEvent( _T("Key up"), event);
401
402 event.Skip();
403 }
404
405 void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
406 {
407 switch ( event.KeyCode() )
408 {
409 case WXK_F1:
410 // show current position and text length
411 {
412 long line, column, pos = GetInsertionPoint();
413 PositionToXY(pos, &column, &line);
414
415 wxLogMessage( _T("Current position: %ld\n"
416 "Current line, column: (%ld, %ld)\n"
417 "Number of lines: %ld\n"
418 "Current line length: %ld\n"
419 "Total text length: %ld"),
420 pos,
421 line, column,
422 GetNumberOfLines(),
423 GetLineLength(line),
424 GetLastPosition());
425 }
426 break;
427
428 case WXK_F2:
429 // go to the end
430 SetInsertionPointEnd();
431 break;
432
433 case WXK_F3:
434 // go to position 10
435 SetInsertionPoint(10);
436 break;
437
438 case WXK_F4:
439 if (!m_hasCapture)
440 {
441 wxLogDebug( wxT("Now capturing mouse and events.") );
442 m_hasCapture = TRUE;
443 CaptureMouse();
444 }
445 else
446 {
447 wxLogDebug( wxT("Stopped capturing mouse and events.") );
448 m_hasCapture = TRUE;
449 ReleaseMouse();
450 }
451 break;
452
453 case WXK_F5:
454 // insert a blank line
455 WriteText("\n");
456 break;
457
458 default:
459 LogEvent( wxT("Key down"), event);
460 }
461
462 event.Skip();
463 }
464
465 //----------------------------------------------------------------------
466 // MyPanel
467 //----------------------------------------------------------------------
468
469 BEGIN_EVENT_TABLE(MyPanel, wxPanel)
470 EVT_SIZE(MyPanel::OnSize)
471 END_EVENT_TABLE()
472
473 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
474 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
475 {
476 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE );
477
478 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
479
480 delete old_log;
481
482 // single line text controls
483
484 m_text = new MyTextCtrl( this, -1, "Single line.",
485 wxPoint(10,10), wxSize(140,-1), 0);
486 (*m_text) << " Appended.";
487 m_text->SetInsertionPoint(0);
488 m_text->WriteText( "Prepended. " );
489
490 m_password = new MyTextCtrl( this, -1, "",
491 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD );
492
493 m_readonly = new MyTextCtrl( this, -1, "Read only",
494 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY );
495
496 // multi line text controls
497
498 m_horizontal = new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
499 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL );
500 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
501 FALSE, "", wxFONTENCODING_KOI8));
502
503 m_multitext = new MyTextCtrl( this, -1, "Multi line.",
504 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE );
505 (*m_multitext) << " Appended.";
506 m_multitext->SetInsertionPoint(0);
507 m_multitext->WriteText( "Prepended. " );
508
509 #if wxUSE_TOOLTIPS
510 m_multitext->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
511 #endif
512
513 m_tab = new MyTextCtrl( this, 100, "Multiline, allow <TAB> processing.",
514 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE | wxTE_PROCESS_TAB );
515 m_tab->SetClientData(_T("tab"));
516
517 m_enter = new MyTextCtrl( this, 100, "Multiline, allow <ENTER> processing.",
518 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE);
519 m_enter->SetClientData(_T("enter"));
520
521 m_textrich = new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
522 "(even under broken Win9x)",
523 wxPoint(450, 10), wxSize(200, 230),
524 wxTE_RICH | wxTE_MULTILINE);
525 }
526
527 void MyPanel::OnSize( wxSizeEvent &event )
528 {
529 wxSize client_area( GetClientSize() );
530 m_log->SetSize( 0, 260, client_area.x, client_area.y - 260 );
531 event.Skip();
532 }
533
534 #if wxUSE_CLIPBOARD
535 void MyPanel::DoPasteFromClipboard()
536 {
537 // On X11, we want to get the data from the primary selection instead
538 // of the normal clipboard (which isn't normal under X11 at all). This
539 // call has no effect under MSW.
540 wxTheClipboard->UsePrimarySelection();
541
542 if (!wxTheClipboard->Open())
543 {
544 *m_log << "Error opening the clipboard.\n";
545 return;
546 }
547 else
548 {
549 *m_log << "Successfully opened the clipboard.\n";
550 }
551
552 wxTextDataObject data;
553
554 if (wxTheClipboard->IsSupported( data.GetFormat() ))
555 {
556 *m_log << "Clipboard supports requested format.\n";
557
558 if (wxTheClipboard->GetData( data ))
559 {
560 *m_log << "Successfully retrieved data from the clipboard.\n";
561 *m_multitext << data.GetText() << "\n";
562 }
563 else
564 {
565 *m_log << "Error getting data from the clipboard.\n";
566 }
567 }
568 else
569 {
570 *m_log << "Clipboard doesn't support requested format.\n";
571 }
572
573 wxTheClipboard->Close();
574
575 *m_log << "Closed the clipboard.\n";
576 }
577
578 void MyPanel::DoCopyToClipboard()
579 {
580 // On X11, we want to get the data from the primary selection instead
581 // of the normal clipboard (which isn't normal under X11 at all). This
582 // call has no effect under MSW.
583 wxTheClipboard->UsePrimarySelection();
584
585 wxString text( m_multitext->GetLineText(0) );
586
587 if (text.IsEmpty())
588 {
589 *m_log << "No text to copy.\n";
590
591 return;
592 }
593
594 if (!wxTheClipboard->Open())
595 {
596 *m_log << "Error opening the clipboard.\n";
597
598 return;
599 }
600 else
601 {
602 *m_log << "Successfully opened the clipboard.\n";
603 }
604
605 wxTextDataObject *data = new wxTextDataObject( text );
606
607 if (!wxTheClipboard->SetData( data ))
608 {
609 *m_log << "Error while copying to the clipboard.\n";
610 }
611 else
612 {
613 *m_log << "Successfully copied data to the clipboard.\n";
614 }
615
616 wxTheClipboard->Close();
617
618 *m_log << "Closed the clipboard.\n";
619 }
620
621 #endif // wxUSE_CLIPBOARD
622
623 void MyPanel::DoMoveToEndOfText()
624 {
625 m_multitext->SetInsertionPointEnd();
626 m_multitext->SetFocus();
627 }
628
629 void MyPanel::DoMoveToEndOfEntry()
630 {
631 m_text->SetInsertionPointEnd();
632 m_text->SetFocus();
633 }
634
635 //----------------------------------------------------------------------
636 // MyFrame
637 //----------------------------------------------------------------------
638
639 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
640 EVT_MENU(TEXT_QUIT, MyFrame::OnQuit)
641 EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout)
642 EVT_MENU(TEXT_SAVE, MyFrame::OnFileSave)
643 EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad)
644 EVT_MENU(TEXT_CLEAR, MyFrame::OnLogClear)
645
646 #if wxUSE_TOOLTIPS
647 EVT_MENU(TEXT_TOOLTIPS_SETDELAY, MyFrame::OnSetTooltipDelay)
648 EVT_MENU(TEXT_TOOLTIPS_ENABLE, MyFrame::OnToggleTooltips)
649 #endif // wxUSE_TOOLTIPS
650
651 #if wxUSE_CLIPBOARD
652 EVT_MENU(TEXT_CLIPBOARD_PASTE, MyFrame::OnPasteFromClipboard)
653 EVT_MENU(TEXT_CLIPBOARD_COPY, MyFrame::OnCopyToClipboard)
654 #endif // wxUSE_CLIPBOARD
655
656 EVT_MENU(TEXT_MOVE_ENDTEXT, MyFrame::OnMoveToEndOfText)
657 EVT_MENU(TEXT_MOVE_ENDENTRY, MyFrame::OnMoveToEndOfEntry)
658
659 EVT_IDLE(MyFrame::OnIdle)
660 END_EVENT_TABLE()
661
662 MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h)
663 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h) )
664 {
665 CreateStatusBar(2);
666
667 m_panel = new MyPanel( this, 10, 10, 300, 100 );
668 }
669
670 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
671 {
672 Close(TRUE);
673 }
674
675 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
676 {
677 wxBeginBusyCursor();
678
679 wxMessageDialog dialog(this,
680 "This is a text control sample. It demonstrates the many different\n"
681 "text control styles, the use of the clipboard, setting and handling\n"
682 "tooltips and intercepting key and char events.\n"
683 "\n"
684 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
685 "About Text Controls",
686 wxOK | wxICON_INFORMATION);
687
688 dialog.ShowModal();
689
690 wxEndBusyCursor();
691 }
692
693 #if wxUSE_TOOLTIPS
694 void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
695 {
696 static long s_delay = 5000;
697
698 wxString delay;
699 delay.Printf( _T("%ld"), s_delay);
700
701 delay = wxGetTextFromUser("Enter delay (in milliseconds)",
702 "Set tooltip delay",
703 delay,
704 this);
705 if ( !delay )
706 return; // cancelled
707
708 wxSscanf(delay, _T("%ld"), &s_delay);
709
710 wxToolTip::SetDelay(s_delay);
711
712 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
713 }
714
715 void MyFrame::OnToggleTooltips(wxCommandEvent& event)
716 {
717 static bool s_enabled = TRUE;
718
719 s_enabled = !s_enabled;
720
721 wxToolTip::Enable(s_enabled);
722
723 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
724 }
725 #endif // tooltips
726
727 void MyFrame::OnLogClear(wxCommandEvent& WXUNUSED(event))
728 {
729 m_panel->m_log->Clear();
730 }
731
732 void MyFrame::OnFileSave(wxCommandEvent& event)
733 {
734 if ( m_panel->m_textrich->SaveFile("dummy.txt") )
735 {
736 #if wxUSE_FILE
737 // verify that the fil length is correct (it wasn't under Win95)
738 wxFile file("dummy.txt");
739 wxLogStatus(this, _T("Successfully saved file "
740 "(text len = %ld, file size = %ld)"),
741 m_panel->m_textrich->GetValue().length(),
742 file.Length());
743 #endif
744 }
745 else
746 wxLogStatus(this, _T("Couldn't save the file"));
747 }
748
749 void MyFrame::OnFileLoad(wxCommandEvent& event)
750 {
751 if ( m_panel->m_textrich->LoadFile("dummy.txt") )
752 wxLogStatus(this, _T("Successfully loaded file"));
753 else
754 wxLogStatus(this, _T("Couldn't load the file"));
755 }
756
757 void MyFrame::OnIdle( wxIdleEvent& event )
758 {
759 // track the window which has the focus in the status bar
760 static wxWindow *s_windowFocus = (wxWindow *)NULL;
761 wxWindow *focus = wxWindow::FindFocus();
762 if ( focus && (focus != s_windowFocus) )
763 {
764 s_windowFocus = focus;
765
766 wxString msg;
767 msg.Printf(
768 #ifdef __WXMSW__
769 _T("Focus: wxWindow = %p, HWND = %08x"),
770 #else
771 _T("Focus: wxWindow = %p"),
772 #endif
773 s_windowFocus
774 #ifdef __WXMSW__
775 , s_windowFocus->GetHWND()
776 #endif
777 );
778
779 SetStatusText(msg);
780 }
781 event.Skip();
782 }