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