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