]> git.saurik.com Git - wxWidgets.git/blame - samples/text/text.cpp
Forgot these.
[wxWidgets.git] / samples / text / text.cpp
CommitLineData
02e8b87f
RR
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
0ddea946
VZ
31#if wxUSE_FILE
32 #include "wx/file.h"
33#endif
34
02e8b87f
RR
35#if wxUSE_TOOLTIPS
36 #include "wx/tooltip.h"
37#endif
38
b6a0d8a2
VZ
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
02e8b87f
RR
47//----------------------------------------------------------------------
48// class definitions
49//----------------------------------------------------------------------
50
51class MyApp: public wxApp
52{
53public:
54 bool OnInit();
55};
56
57// a text ctrl which allows to call different wxTextCtrl functions
58// interactively by pressing function keys in it
59class MyTextCtrl : public wxTextCtrl
60{
61public:
62 MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value,
63 const wxPoint &pos, const wxSize &size, int style = 0)
f152cf5a 64 : wxTextCtrl(parent, id, value, pos, size, style) { m_hasCapture = FALSE; }
02e8b87f
RR
65
66 void OnKeyDown(wxKeyEvent& event);
67 void OnKeyUp(wxKeyEvent& event);
68 void OnChar(wxKeyEvent& event);
69
f152cf5a
RR
70 bool m_hasCapture;
71
02e8b87f
RR
72private:
73 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
74 void LogEvent(const wxChar *name, wxKeyEvent& event) const;
75
76 DECLARE_EVENT_TABLE()
77};
78
79class MyPanel: public wxPanel
80{
81public:
82 MyPanel(wxFrame *frame, int x, int y, int w, int h);
83
b6a0d8a2
VZ
84#if wxUSE_CLIPBOARD
85 void DoPasteFromClipboard();
86 void DoCopyToClipboard();
87#endif // wxUSE_CLIPBOARD
88
89 void DoMoveToEndOfText();
90 void DoMoveToEndOfEntry();
02e8b87f 91
89124b1a
RR
92 void OnSize( wxSizeEvent &event );
93
02e8b87f
RR
94 MyTextCtrl *m_text;
95 MyTextCtrl *m_password;
96 MyTextCtrl *m_enter;
97 MyTextCtrl *m_tab;
02e8b87f 98 MyTextCtrl *m_readonly;
bb69661b 99
02e8b87f
RR
100 MyTextCtrl *m_multitext;
101 MyTextCtrl *m_horizontal;
02e8b87f 102
0ddea946
VZ
103 MyTextCtrl *m_textrich;
104
02e8b87f 105 wxTextCtrl *m_log;
89124b1a
RR
106
107private:
108 DECLARE_EVENT_TABLE()
02e8b87f
RR
109};
110
111class MyFrame: public wxFrame
112{
113public:
b6a0d8a2 114 MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h);
02e8b87f
RR
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
b6a0d8a2
VZ
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
e66ad5c6 135 void OnLogClear(wxCommandEvent& event);
0ddea946 136 void OnFileSave(wxCommandEvent& event);
b6a0d8a2
VZ
137 void OnFileLoad(wxCommandEvent& event);
138
02e8b87f
RR
139 void OnIdle( wxIdleEvent& event );
140
141private:
b6a0d8a2
VZ
142 MyPanel *m_panel;
143
02e8b87f
RR
144 DECLARE_EVENT_TABLE()
145};
146
147//----------------------------------------------------------------------
148// main()
149//----------------------------------------------------------------------
150
151IMPLEMENT_APP(MyApp)
152
153//----------------------------------------------------------------------
154// MyApp
155//----------------------------------------------------------------------
156
157enum
158{
b6a0d8a2
VZ
159 TEXT_QUIT = 100,
160 TEXT_ABOUT,
161 TEXT_LOAD,
0ddea946 162 TEXT_SAVE,
e66ad5c6 163 TEXT_CLEAR,
b6a0d8a2
VZ
164
165 // clipboard menu
166 TEXT_CLIPBOARD_COPY = 200,
167 TEXT_CLIPBOARD_PASTE,
02e8b87f
RR
168
169 // tooltip menu
b6a0d8a2
VZ
170 TEXT_TOOLTIPS_SETDELAY = 300,
171 TEXT_TOOLTIPS_ENABLE,
172
173 // move menu
174 TEXT_MOVE_ENDTEXT = 400,
175 TEXT_MOVE_ENDENTRY
02e8b87f
RR
176};
177
178bool MyApp::OnInit()
179{
180 // Create the main frame window
181 MyFrame *frame = new MyFrame((wxFrame *) NULL,
0ddea946 182 "Text wxWindows sample", 50, 50, 660, 420);
89124b1a 183 frame->SetSizeHints( 500, 400 );
02e8b87f 184
02e8b87f 185 wxMenu *file_menu = new wxMenu;
e66ad5c6
VZ
186 file_menu->Append(TEXT_CLEAR, "&Clear the log\tCtrl-C",
187 "Clear the log window contents");
0ddea946
VZ
188 file_menu->Append(TEXT_SAVE, "&Save file\tCtrl-S",
189 "Save the text control contents to file");
b6a0d8a2
VZ
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");
02e8b87f
RR
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;
b6a0d8a2 202 tooltip_menu->Append(TEXT_TOOLTIPS_SETDELAY, "Set &delay\tCtrl-D");
02e8b87f 203 tooltip_menu->AppendSeparator();
b6a0d8a2 204 tooltip_menu->Append(TEXT_TOOLTIPS_ENABLE, "&Toggle tooltips\tCtrl-T",
02e8b87f 205 "enable/disable tooltips", TRUE);
b6a0d8a2 206 tooltip_menu->Check(TEXT_TOOLTIPS_ENABLE, TRUE);
02e8b87f
RR
207 menu_bar->Append(tooltip_menu, "&Tooltips");
208#endif // wxUSE_TOOLTIPS
209
b6a0d8a2
VZ
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
02e8b87f
RR
224 frame->SetMenuBar(menu_bar);
225
226 frame->Show(TRUE);
227
228 SetTopWindow(frame);
229
230 // report success
231 return TRUE;
232}
233
234//----------------------------------------------------------------------
235// MyTextCtrl
236//----------------------------------------------------------------------
237
238BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
239 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
240 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
241 EVT_CHAR(MyTextCtrl::OnChar)
242END_EVENT_TABLE()
243
244void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const
245{
246 wxString key;
247 long keycode = event.KeyCode();
02e8b87f
RR
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
4afd7529 353 default:
89124b1a
RR
354 {
355 if ( wxIsprint((int)keycode) )
356 key.Printf( _T("'%c'") , (char)keycode);
357 else
4afd7529
VZ
358 key.Printf( _T("unknown (%ld)"), keycode);
359 }
02e8b87f
RR
360 }
361 }
362
363 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
364 name,
365 key.c_str(),
366 GetChar( event.ControlDown(), _T('C') ),
367 GetChar( event.AltDown(), _T('A') ),
368 GetChar( event.ShiftDown(), _T('S') ),
369 GetChar( event.MetaDown(), _T('M') ) );
370
371}
372
373void MyTextCtrl::OnChar(wxKeyEvent& event)
374{
375 LogEvent( _T("Char"), event);
376
3017f78d
RR
377/* How are we supposed to test wxTE_PROCESS_TAB with this code?
378
b6a0d8a2
VZ
379 if ( event.KeyCode() == WXK_TAB )
380 {
381 WriteText("\t");
382 }
383 else
384 {
385 event.Skip();
386 }
3017f78d
RR
387*/
388 event.Skip();
02e8b87f
RR
389}
390
391void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
392{
393 LogEvent( _("Key up"), event);
394
395 event.Skip();
396}
397
398void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
399{
400 switch ( event.KeyCode() )
401 {
402 case WXK_F1:
403 // show current position and text length
404 {
405 long line, column, pos = GetInsertionPoint();
406 PositionToXY(pos, &column, &line);
407
408 wxLogMessage( _T("Current position: %ld\n"
409 "Current line, column: (%ld, %ld)\n"
410 "Number of lines: %ld\n"
411 "Current line length: %ld\n"
412 "Total text length: %ld"),
413 pos,
414 line, column,
415 GetNumberOfLines(),
416 GetLineLength(line),
417 GetLastPosition());
418 }
419 break;
420
421 case WXK_F2:
422 // go to the end
423 SetInsertionPointEnd();
424 break;
425
426 case WXK_F3:
427 // go to position 10
428 SetInsertionPoint(10);
429 break;
4afd7529 430
f152cf5a 431 case WXK_F4:
4afd7529
VZ
432 if (!m_hasCapture)
433 {
f152cf5a 434 wxLogDebug( wxT("Now capturing mouse and events.") );
4afd7529
VZ
435 m_hasCapture = TRUE;
436 CaptureMouse();
437 }
438 else
439 {
f152cf5a 440 wxLogDebug( wxT("Stopped capturing mouse and events.") );
4afd7529
VZ
441 m_hasCapture = TRUE;
442 ReleaseMouse();
443 }
f152cf5a 444 break;
4afd7529
VZ
445
446 case WXK_F5:
447 // insert a blank line
448 WriteText("\n");
0ddea946 449 break;
02e8b87f 450
0ddea946
VZ
451 default:
452 LogEvent( _("Key down"), event);
453 }
02e8b87f
RR
454
455 event.Skip();
456}
457
458//----------------------------------------------------------------------
459// MyPanel
460//----------------------------------------------------------------------
461
89124b1a
RR
462BEGIN_EVENT_TABLE(MyPanel, wxPanel)
463 EVT_SIZE(MyPanel::OnSize)
464END_EVENT_TABLE()
465
02e8b87f
RR
466MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
467 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
468{
469 m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE );
470
89124b1a
RR
471 wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
472
473 delete old_log;
02e8b87f
RR
474
475 // single line text controls
476
bb69661b 477 m_text = new MyTextCtrl( this, -1, "Single line.",
02e8b87f
RR
478 wxPoint(10,10), wxSize(140,-1), 0);
479 (*m_text) << " Appended.";
480 m_text->SetInsertionPoint(0);
481 m_text->WriteText( "Prepended. " );
bb69661b
VZ
482
483 m_password = new MyTextCtrl( this, -1, "",
02e8b87f 484 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD );
bb69661b
VZ
485
486 m_readonly = new MyTextCtrl( this, -1, "Read only",
02e8b87f 487 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY );
bb69661b 488
02e8b87f
RR
489 // multi line text controls
490
bb69661b
VZ
491 m_horizontal = new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.",
492 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL );
ddc8c2e3
VZ
493 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
494 FALSE, "", wxFONTENCODING_KOI8));
bb69661b 495
5e3dad49 496 m_multitext = new MyTextCtrl( this, -1, "Multi line.",
02e8b87f
RR
497 wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE );
498 (*m_multitext) << " Appended.";
499 m_multitext->SetInsertionPoint(0);
500 m_multitext->WriteText( "Prepended. " );
bb69661b 501
02e8b87f 502#if wxUSE_TOOLTIPS
f152cf5a 503 m_multitext->SetToolTip("Press F1 here for statitics, F4 for capture and uncapture mouse.");
02e8b87f
RR
504#endif
505
bb69661b 506 m_tab = new MyTextCtrl( this, -1, "Multiline, allow <TAB> processing.",
02e8b87f
RR
507 wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE | wxTE_PROCESS_TAB );
508
b6a0d8a2
VZ
509 m_enter = new MyTextCtrl( this, -1, "Multiline, allow <ENTER> processing.",
510 wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE);
0ddea946
VZ
511
512 m_textrich = new MyTextCtrl(this, -1, "Allows more than 30Kb of text\n"
513 "(even under broken Win9x)",
514 wxPoint(450, 10), wxSize(200, 230),
515 wxTE_RICH | wxTE_MULTILINE);
02e8b87f
RR
516}
517
89124b1a
RR
518void MyPanel::OnSize( wxSizeEvent &event )
519{
520 wxSize client_area( GetClientSize() );
521 m_log->SetSize( 0, 260, client_area.x, client_area.y - 260 );
522 event.Skip();
523}
524
b6a0d8a2
VZ
525#if wxUSE_CLIPBOARD
526void MyPanel::DoPasteFromClipboard()
02e8b87f 527{
7e2c43b8
RR
528 // On X11, we want to get the data from the primary selection instead
529 // of the normal clipboard (which isn't normal under X11 at all). This
530 // call has no effect under MSW.
531 wxTheClipboard->UsePrimarySelection();
532
02e8b87f
RR
533 if (!wxTheClipboard->Open())
534 {
535 *m_log << "Error opening the clipboard.\n";
02e8b87f
RR
536 return;
537 }
538 else
539 {
540 *m_log << "Successfully opened the clipboard.\n";
541 }
542
543 wxTextDataObject data;
544
545 if (wxTheClipboard->IsSupported( data.GetFormat() ))
546 {
547 *m_log << "Clipboard supports requested format.\n";
548
11e1c70d 549 if (wxTheClipboard->GetData( data ))
02e8b87f
RR
550 {
551 *m_log << "Successfully retrieved data from the clipboard.\n";
552 *m_multitext << data.GetText() << "\n";
553 }
554 else
555 {
556 *m_log << "Error getting data from the clipboard.\n";
557 }
558 }
559 else
560 {
561 *m_log << "Clipboard doesn't support requested format.\n";
562 }
563
564 wxTheClipboard->Close();
565
566 *m_log << "Closed the clipboard.\n";
02e8b87f
RR
567}
568
b6a0d8a2 569void MyPanel::DoCopyToClipboard()
02e8b87f 570{
eff869aa
RR
571 // On X11, we want to get the data from the primary selection instead
572 // of the normal clipboard (which isn't normal under X11 at all). This
573 // call has no effect under MSW.
574 wxTheClipboard->UsePrimarySelection();
575
02e8b87f
RR
576 wxString text( m_multitext->GetLineText(0) );
577
578 if (text.IsEmpty())
579 {
580 *m_log << "No text to copy.\n";
581
582 return;
583 }
584
585 if (!wxTheClipboard->Open())
586 {
587 *m_log << "Error opening the clipboard.\n";
588
589 return;
590 }
591 else
592 {
593 *m_log << "Successfully opened the clipboard.\n";
594 }
595
596 wxTextDataObject *data = new wxTextDataObject( text );
597
598 if (!wxTheClipboard->SetData( data ))
599 {
600 *m_log << "Error while copying to the clipboard.\n";
601 }
602 else
603 {
604 *m_log << "Successfully copied data to the clipboard.\n";
605 }
606
607 wxTheClipboard->Close();
608
609 *m_log << "Closed the clipboard.\n";
02e8b87f
RR
610}
611
b6a0d8a2
VZ
612#endif // wxUSE_CLIPBOARD
613
614void MyPanel::DoMoveToEndOfText()
02e8b87f
RR
615{
616 m_multitext->SetInsertionPointEnd();
617 m_multitext->SetFocus();
618}
619
b6a0d8a2 620void MyPanel::DoMoveToEndOfEntry()
02e8b87f
RR
621{
622 m_text->SetInsertionPointEnd();
623 m_text->SetFocus();
624}
625
626//----------------------------------------------------------------------
627// MyFrame
628//----------------------------------------------------------------------
629
630BEGIN_EVENT_TABLE(MyFrame, wxFrame)
b6a0d8a2
VZ
631 EVT_MENU(TEXT_QUIT, MyFrame::OnQuit)
632 EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout)
0ddea946 633 EVT_MENU(TEXT_SAVE, MyFrame::OnFileSave)
b6a0d8a2 634 EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad)
e66ad5c6 635 EVT_MENU(TEXT_CLEAR, MyFrame::OnLogClear)
b6a0d8a2 636
02e8b87f 637#if wxUSE_TOOLTIPS
b6a0d8a2
VZ
638 EVT_MENU(TEXT_TOOLTIPS_SETDELAY, MyFrame::OnSetTooltipDelay)
639 EVT_MENU(TEXT_TOOLTIPS_ENABLE, MyFrame::OnToggleTooltips)
02e8b87f 640#endif // wxUSE_TOOLTIPS
b6a0d8a2 641
ee31c392 642#if wxUSE_CLIPBOARD
b6a0d8a2
VZ
643 EVT_MENU(TEXT_CLIPBOARD_PASTE, MyFrame::OnPasteFromClipboard)
644 EVT_MENU(TEXT_CLIPBOARD_COPY, MyFrame::OnCopyToClipboard)
ee31c392
VZ
645#endif // wxUSE_CLIPBOARD
646
b6a0d8a2
VZ
647 EVT_MENU(TEXT_MOVE_ENDTEXT, MyFrame::OnMoveToEndOfText)
648 EVT_MENU(TEXT_MOVE_ENDENTRY, MyFrame::OnMoveToEndOfEntry)
649
650 EVT_IDLE(MyFrame::OnIdle)
02e8b87f
RR
651END_EVENT_TABLE()
652
b6a0d8a2
VZ
653MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h)
654 : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h) )
02e8b87f
RR
655{
656 CreateStatusBar(2);
657
b6a0d8a2 658 m_panel = new MyPanel( this, 10, 10, 300, 100 );
02e8b87f
RR
659}
660
661void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
662{
663 Close(TRUE);
664}
665
666void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
667{
668 wxBeginBusyCursor();
669
b6a0d8a2
VZ
670 wxMessageDialog dialog(this,
671 "This is a text control sample. It demonstrates the many different\n"
672 "text control styles, the use of the clipboard, setting and handling\n"
673 "tooltips and intercepting key and char events.\n"
674 "\n"
675 "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin",
676 "About Text Controls",
677 wxOK | wxICON_INFORMATION);
678
02e8b87f
RR
679 dialog.ShowModal();
680
681 wxEndBusyCursor();
682}
683
684#if wxUSE_TOOLTIPS
685void MyFrame::OnSetTooltipDelay(wxCommandEvent& event)
686{
687 static long s_delay = 5000;
688
689 wxString delay;
690 delay.Printf( _T("%ld"), s_delay);
691
692 delay = wxGetTextFromUser("Enter delay (in milliseconds)",
bb69661b
VZ
693 "Set tooltip delay",
694 delay,
695 this);
02e8b87f
RR
696 if ( !delay )
697 return; // cancelled
698
699 wxSscanf(delay, _T("%ld"), &s_delay);
700
701 wxToolTip::SetDelay(s_delay);
702
703 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
704}
705
706void MyFrame::OnToggleTooltips(wxCommandEvent& event)
707{
708 static bool s_enabled = TRUE;
709
710 s_enabled = !s_enabled;
711
712 wxToolTip::Enable(s_enabled);
713
714 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
715}
716#endif // tooltips
717
e66ad5c6
VZ
718void MyFrame::OnLogClear(wxCommandEvent& WXUNUSED(event))
719{
720 m_panel->m_log->Clear();
721}
722
0ddea946
VZ
723void MyFrame::OnFileSave(wxCommandEvent& event)
724{
725 if ( m_panel->m_textrich->SaveFile("dummy.txt") )
726 {
727#if wxUSE_FILE
728 // verify that the fil length is correct (it wasn't under Win95)
729 wxFile file("dummy.txt");
730 wxLogStatus(this, _T("Successfully saved file "
731 "(text len = %ld, file size = %ld)"),
732 m_panel->m_textrich->GetValue().length(),
733 file.Length());
734#endif
735 }
736 else
737 wxLogStatus(this, _T("Couldn't save the file"));
738}
739
b6a0d8a2
VZ
740void MyFrame::OnFileLoad(wxCommandEvent& event)
741{
0ddea946 742 if ( m_panel->m_textrich->LoadFile("dummy.txt") )
5e3dad49
VZ
743 wxLogStatus(this, _T("Successfully loaded file"));
744 else
745 wxLogStatus(this, _T("Couldn't load the file"));
b6a0d8a2
VZ
746}
747
02e8b87f
RR
748void MyFrame::OnIdle( wxIdleEvent& event )
749{
750 // track the window which has the focus in the status bar
751 static wxWindow *s_windowFocus = (wxWindow *)NULL;
752 wxWindow *focus = wxWindow::FindFocus();
753 if ( focus && (focus != s_windowFocus) )
754 {
755 s_windowFocus = focus;
756
757 wxString msg;
758 msg.Printf(
759#ifdef __WXMSW__
760 _T("Focus: wxWindow = %p, HWND = %08x"),
761#else
762 _T("Focus: wxWindow = %p"),
763#endif
764 s_windowFocus
765#ifdef __WXMSW__
766 , s_windowFocus->GetHWND()
767#endif
768 );
769
770 SetStatusText(msg);
771 }
772 event.Skip();
773}