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