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