]> git.saurik.com Git - wxWidgets.git/blob - samples/text/text.cpp
3908b8e4cdbffb9240b461540a80f0212d1fecfc
[wxWidgets.git] / samples / text / text.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: text.cpp
3 // Purpose: TextCtrl wxWidgets 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 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21
22 #if wxUSE_CLIPBOARD
23 #include "wx/dataobj.h"
24 #include "wx/clipbrd.h"
25 #endif
26
27 #if wxUSE_FILE
28 #include "wx/file.h"
29 #endif
30
31 #if wxUSE_TOOLTIPS
32 #include "wx/tooltip.h"
33 #endif
34
35 // We test for wxUSE_DRAG_AND_DROP also, because data objects may not be
36 // implemented for compilers that can't cope with the OLE parts in
37 // wxUSE_DRAG_AND_DROP.
38 #if !wxUSE_DRAG_AND_DROP
39 #undef wxUSE_CLIPBOARD
40 #define wxUSE_CLIPBOARD 0
41 #endif
42
43 #include "wx/colordlg.h"
44 #include "wx/fontdlg.h"
45 #include "wx/numdlg.h"
46 #include "wx/tokenzr.h"
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)
66 {
67 m_hasCapture = false;
68 }
69
70 void OnKeyDown(wxKeyEvent& event);
71 void OnKeyUp(wxKeyEvent& event);
72 void OnChar(wxKeyEvent& event);
73
74 void OnText(wxCommandEvent& event);
75 void OnTextEnter(wxCommandEvent& event);
76 void OnTextURL(wxTextUrlEvent& event);
77 void OnTextMaxLen(wxCommandEvent& event);
78
79 void OnTextCut(wxClipboardTextEvent & event);
80 void OnTextCopy(wxClipboardTextEvent & event);
81 void OnTextPaste(wxClipboardTextEvent & event);
82
83 void OnMouseEvent(wxMouseEvent& event);
84
85 void OnSetFocus(wxFocusEvent& event);
86 void OnKillFocus(wxFocusEvent& event);
87
88 static bool ms_logKey;
89 static bool ms_logChar;
90 static bool ms_logMouse;
91 static bool ms_logText;
92 static bool ms_logFocus;
93 static bool ms_logClip;
94
95 private:
96 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
97
98 void LogKeyEvent(const wxChar *name, wxKeyEvent& event) const;
99 void LogClipEvent(const wxChar *what, wxClipboardTextEvent& event);
100
101 bool m_hasCapture;
102
103 DECLARE_EVENT_TABLE()
104 };
105
106 class MyPanel: public wxPanel
107 {
108 public:
109 MyPanel(wxFrame *frame, int x, int y, int w, int h);
110 virtual ~MyPanel()
111 {
112 #if wxUSE_LOG
113 delete wxLog::SetActiveTarget(m_logOld);
114 #endif // wxUSE_LOG
115 }
116
117 #if wxUSE_CLIPBOARD
118 void DoPasteFromClipboard();
119 void DoCopyToClipboard();
120 #endif // wxUSE_CLIPBOARD
121
122 void DoRemoveText();
123 void DoReplaceText();
124 void DoSelectText();
125 void DoMoveToEndOfText();
126 void DoMoveToEndOfEntry();
127
128 // return true if currently text control has any selection
129 bool HasSelection() const
130 {
131 long from, to;
132 GetFocusedText()->GetSelection(&from, &to);
133 return from != to;
134 }
135
136 MyTextCtrl *m_text;
137 MyTextCtrl *m_password;
138 MyTextCtrl *m_enter;
139 MyTextCtrl *m_tab;
140 MyTextCtrl *m_readonly;
141 MyTextCtrl *m_limited;
142
143 MyTextCtrl *m_multitext;
144 MyTextCtrl *m_horizontal;
145
146 MyTextCtrl *m_textrich;
147
148 #if wxUSE_LOG
149 wxTextCtrl *m_log;
150 wxLog *m_logOld;
151 #endif // wxUSE_LOG
152
153 private:
154 // get the currently focused text control or return the default one
155 // (m_multitext) is no text ctrl has focus -- in any case, returns
156 // something non NULL
157 wxTextCtrl *GetFocusedText() const;
158 };
159
160 class MyFrame: public wxFrame
161 {
162 public:
163 MyFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
164
165 void OnQuit(wxCommandEvent& event);
166 void OnAbout(wxCommandEvent& event);
167 #if wxUSE_TOOLTIPS
168 void OnSetTooltipDelay(wxCommandEvent& event);
169 void OnToggleTooltips(wxCommandEvent& event);
170 #endif // wxUSE_TOOLTIPS
171
172 #if wxUSE_CLIPBOARD
173 void OnPasteFromClipboard( wxCommandEvent& WXUNUSED(event) )
174 {
175 wxLogMessage(_T("Pasting text from clipboard."));
176 m_panel->DoPasteFromClipboard();
177 }
178 void OnCopyToClipboard( wxCommandEvent& WXUNUSED(event) )
179 {
180 wxLogMessage(_T("Copying text to clipboard."));
181 m_panel->DoCopyToClipboard();
182 }
183
184 void OnUpdatePasteFromClipboard(wxUpdateUIEvent& event)
185 {
186 wxClipboardLocker lockClip;
187
188 event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) );
189 }
190
191 void OnUpdateCopyToClipboard(wxUpdateUIEvent& event)
192 {
193 event.Enable( m_panel->HasSelection() );
194 }
195 #endif // wxUSE_CLIPBOARD
196
197 void OnAddTextFreeze( wxCommandEvent& WXUNUSED(event) )
198 { DoAddText(true); }
199 void OnAddText( wxCommandEvent& WXUNUSED(event) )
200 { DoAddText(false); }
201 void OnRemoveText( wxCommandEvent& WXUNUSED(event) )
202 { m_panel->DoRemoveText(); }
203 void OnReplaceText( wxCommandEvent& WXUNUSED(event) )
204 { m_panel->DoReplaceText(); }
205 void OnSelectText( wxCommandEvent& WXUNUSED(event) )
206 { m_panel->DoSelectText(); }
207
208 void OnMoveToEndOfText( wxCommandEvent& WXUNUSED(event) )
209 { m_panel->DoMoveToEndOfText(); }
210 void OnMoveToEndOfEntry( wxCommandEvent& WXUNUSED(event) )
211 { m_panel->DoMoveToEndOfEntry(); }
212
213 void OnScrollLineDown(wxCommandEvent& WXUNUSED(event))
214 {
215 if ( !m_panel->m_textrich->LineDown() )
216 wxLogMessage(_T("Already at the bottom"));
217 }
218
219 void OnScrollLineUp(wxCommandEvent& WXUNUSED(event))
220 {
221 if ( !m_panel->m_textrich->LineUp() )
222 wxLogMessage(_T("Already at the top"));
223 }
224
225 void OnScrollPageDown(wxCommandEvent& WXUNUSED(event))
226 {
227 if ( !m_panel->m_textrich->PageDown() )
228 wxLogMessage(_T("Already at the bottom"));
229 }
230
231 void OnScrollPageUp(wxCommandEvent& WXUNUSED(event))
232 {
233 if ( !m_panel->m_textrich->PageUp() )
234 wxLogMessage(_T("Already at the top"));
235 }
236
237 void OnGetLine(wxCommandEvent& WXUNUSED(event))
238 {
239 long nLine = wxGetNumberFromUser(wxT("Which line would you like to get?"),
240 wxT("Enter which line you would like to get"),
241 wxT("Get a line from the tabbed multiline text control") );
242
243 wxMessageBox(m_panel->m_tab->GetLineText(nLine));
244 }
245
246 void OnGetLineLength(wxCommandEvent& WXUNUSED(event))
247 {
248 long nLine = wxGetNumberFromUser(wxT("Which line would you like to get?"),
249 wxT("Enter which line you would like to get"),
250 wxT("Get length of a line from the tabbed multiline text control") );
251
252 wxMessageBox(wxString::Format(wxT("Length of line %i is:%i"),
253 (int) nLine,
254 m_panel->m_tab->GetLineLength(nLine))
255 );
256 }
257
258 #if wxUSE_LOG
259 void OnLogClear(wxCommandEvent& event);
260 #endif // wxUSE_LOG
261 void OnFileSave(wxCommandEvent& event);
262 void OnFileLoad(wxCommandEvent& event);
263 void OnRichTextTest(wxCommandEvent& event);
264
265 void OnSetEditable(wxCommandEvent& event);
266 void OnSetEnabled(wxCommandEvent& event);
267
268 void OnLogKey(wxCommandEvent& event)
269 {
270 MyTextCtrl::ms_logKey = event.IsChecked();
271 }
272
273 void OnLogChar(wxCommandEvent& event)
274 {
275 MyTextCtrl::ms_logChar = event.IsChecked();
276 }
277
278 void OnLogMouse(wxCommandEvent& event)
279 {
280 MyTextCtrl::ms_logMouse = event.IsChecked();
281 }
282
283 void OnLogText(wxCommandEvent& event)
284 {
285 MyTextCtrl::ms_logText = event.IsChecked();
286 }
287
288 void OnLogFocus(wxCommandEvent& event)
289 {
290 MyTextCtrl::ms_logFocus = event.IsChecked();
291 }
292
293 void OnLogClip(wxCommandEvent& event)
294 {
295 MyTextCtrl::ms_logClip = event.IsChecked();
296 }
297
298 void OnSetText(wxCommandEvent& WXUNUSED(event))
299 {
300 m_panel->m_text->SetValue(_T("Hello, world! (what else did you expect?)"));
301 }
302
303 void OnChangeText(wxCommandEvent& WXUNUSED(event))
304 {
305 m_panel->m_text->ChangeValue(_T("Changed, not set: no event"));
306 }
307
308 void OnIdle( wxIdleEvent& event );
309
310 private:
311 void DoAddText(bool freeze)
312 {
313 wxTextCtrl * const text = m_panel->m_textrich;
314
315 if ( freeze )
316 text->Freeze();
317
318 text->Clear();
319
320 for ( int i = 0; i < 100; i++ )
321 {
322 text->AppendText(wxString::Format(wxT("Line %i\n"), i));
323 }
324
325 text->SetInsertionPoint(0);
326
327 if ( freeze )
328 text->Thaw();
329 }
330
331 MyPanel *m_panel;
332
333 DECLARE_EVENT_TABLE()
334 };
335
336 /*
337 * RichTextFrame is used to demonstrate rich text behaviour
338 */
339
340 class RichTextFrame: public wxFrame
341 {
342 public:
343 RichTextFrame(wxWindow* parent, const wxString& title);
344
345 // Event handlers
346
347 void OnClose(wxCommandEvent& event);
348 void OnIdle(wxIdleEvent& event);
349 void OnLeftAlign(wxCommandEvent& event);
350 void OnRightAlign(wxCommandEvent& event);
351 void OnJustify(wxCommandEvent& event);
352 void OnCentre(wxCommandEvent& event);
353 void OnChangeFont(wxCommandEvent& event);
354 void OnChangeTextColour(wxCommandEvent& event);
355 void OnChangeBackgroundColour(wxCommandEvent& event);
356 void OnLeftIndent(wxCommandEvent& event);
357 void OnRightIndent(wxCommandEvent& event);
358 void OnTabStops(wxCommandEvent& event);
359
360 private:
361 wxTextCtrl *m_textCtrl;
362 long m_currentPosition;
363
364 DECLARE_EVENT_TABLE()
365 };
366
367 //----------------------------------------------------------------------
368 // main()
369 //----------------------------------------------------------------------
370
371 IMPLEMENT_APP(MyApp)
372
373 //----------------------------------------------------------------------
374 // MyApp
375 //----------------------------------------------------------------------
376
377 enum
378 {
379 TEXT_QUIT = wxID_EXIT,
380 TEXT_ABOUT = wxID_ABOUT,
381 TEXT_LOAD = 101,
382 TEXT_SAVE,
383 TEXT_CLEAR,
384 TEXT_RICH_TEXT_TEST,
385
386 // clipboard menu
387 TEXT_CLIPBOARD_COPY = 200,
388 TEXT_CLIPBOARD_PASTE,
389 TEXT_CLIPBOARD_VETO,
390
391 // tooltip menu
392 TEXT_TOOLTIPS_SETDELAY = 300,
393 TEXT_TOOLTIPS_ENABLE,
394
395 // text menu
396 TEXT_ADD_SOME = 400,
397 TEXT_ADD_FREEZE,
398 TEXT_MOVE_ENDTEXT,
399 TEXT_MOVE_ENDENTRY,
400 TEXT_SET_EDITABLE,
401 TEXT_SET_ENABLED,
402 TEXT_LINE_DOWN,
403 TEXT_LINE_UP,
404 TEXT_PAGE_DOWN,
405 TEXT_PAGE_UP,
406
407 TEXT_GET_LINE,
408 TEXT_GET_LINELENGTH,
409
410 TEXT_REMOVE,
411 TEXT_REPLACE,
412 TEXT_SELECT,
413 TEXT_SET,
414 TEXT_CHANGE,
415
416 // log menu
417 TEXT_LOG_KEY,
418 TEXT_LOG_CHAR,
419 TEXT_LOG_MOUSE,
420 TEXT_LOG_TEXT,
421 TEXT_LOG_FOCUS,
422 TEXT_LOG_CLIP,
423
424 TEXT_END
425 };
426
427 bool MyApp::OnInit()
428 {
429 if ( !wxApp::OnInit() )
430 return false;
431
432 // Create the main frame window
433 MyFrame *frame = new MyFrame((wxFrame *) NULL,
434 _T("Text wxWidgets sample"), 50, 50, 700, 550);
435 frame->SetSizeHints( 500, 400 );
436
437 wxMenu *file_menu = new wxMenu;
438 file_menu->Append(TEXT_SAVE, _T("&Save file\tCtrl-S"),
439 _T("Save the text control contents to file"));
440 file_menu->Append(TEXT_LOAD, _T("&Load file\tCtrl-O"),
441 _T("Load the sample file into text control"));
442 file_menu->AppendSeparator();
443 file_menu->Append(TEXT_RICH_TEXT_TEST, _T("Show Rich Text Editor"));
444 file_menu->AppendSeparator();
445 file_menu->Append(TEXT_ABOUT, _T("&About\tAlt-A"));
446 file_menu->AppendSeparator();
447 file_menu->Append(TEXT_QUIT, _T("E&xit\tAlt-X"), _T("Quit this sample"));
448
449 wxMenuBar *menu_bar = new wxMenuBar( wxMB_DOCKABLE );
450 menu_bar->Append(file_menu, _T("&File"));
451
452 #if wxUSE_TOOLTIPS
453 wxMenu *tooltip_menu = new wxMenu;
454 tooltip_menu->Append(TEXT_TOOLTIPS_SETDELAY, _T("Set &delay\tCtrl-D"));
455 tooltip_menu->AppendSeparator();
456 tooltip_menu->Append(TEXT_TOOLTIPS_ENABLE, _T("&Toggle tooltips\tCtrl-T"),
457 _T("enable/disable tooltips"), true);
458 tooltip_menu->Check(TEXT_TOOLTIPS_ENABLE, true);
459 menu_bar->Append(tooltip_menu, _T("&Tooltips"));
460 #endif // wxUSE_TOOLTIPS
461
462 #if wxUSE_CLIPBOARD
463 // notice that we use non default accelerators on purpose here to compare
464 // their behaviour with the built in handling of standard Ctrl/Cmd-C/V
465 wxMenu *menuClipboard = new wxMenu;
466 menuClipboard->Append(TEXT_CLIPBOARD_COPY, _T("&Copy\tCtrl-Shift-C"),
467 _T("Copy the selection to the clipboard"));
468 menuClipboard->Append(TEXT_CLIPBOARD_PASTE, _T("&Paste\tCtrl-Shift-V"),
469 _T("Paste from clipboard to the text control"));
470 menuClipboard->AppendSeparator();
471 menuClipboard->AppendCheckItem(TEXT_CLIPBOARD_VETO, _T("Vet&o\tCtrl-Shift-O"),
472 _T("Veto all clipboard operations"));
473 menu_bar->Append(menuClipboard, _T("&Clipboard"));
474 #endif // wxUSE_CLIPBOARD
475
476 wxMenu *menuText = new wxMenu;
477 menuText->Append(TEXT_ADD_SOME, _T("&Append some text\tCtrl-A"));
478 menuText->Append(TEXT_ADD_FREEZE, _T("&Append text with freeze/thaw\tShift-Ctrl-A"));
479 menuText->Append(TEXT_REMOVE, _T("&Remove first 10 characters\tCtrl-Y"));
480 menuText->Append(TEXT_REPLACE, _T("&Replace characters 4 to 8 with ABC\tCtrl-R"));
481 menuText->Append(TEXT_SELECT, _T("&Select characters 4 to 8\tCtrl-I"));
482 menuText->Append(TEXT_SET, _T("&Set the first text zone value\tCtrl-E"));
483 menuText->Append(TEXT_CHANGE, _T("&Change the first text zone value\tShift-Ctrl-E"));
484 menuText->AppendSeparator();
485 menuText->Append(TEXT_MOVE_ENDTEXT, _T("Move cursor to the end of &text"));
486 menuText->Append(TEXT_MOVE_ENDENTRY, _T("Move cursor to the end of &entry"));
487 menuText->AppendCheckItem(TEXT_SET_EDITABLE, _T("Toggle &editable state"));
488 menuText->AppendCheckItem(TEXT_SET_ENABLED, _T("Toggle e&nabled state"));
489 menuText->Check(TEXT_SET_EDITABLE, true);
490 menuText->Check(TEXT_SET_ENABLED, true);
491 menuText->AppendSeparator();
492 menuText->Append(TEXT_LINE_DOWN, _T("Scroll text one line down"));
493 menuText->Append(TEXT_LINE_UP, _T("Scroll text one line up"));
494 menuText->Append(TEXT_PAGE_DOWN, _T("Scroll text one page down"));
495 menuText->Append(TEXT_PAGE_UP, _T("Scroll text one page up"));
496 menuText->AppendSeparator();
497 menuText->Append(TEXT_GET_LINE, _T("Get the text of a line of the tabbed multiline"));
498 menuText->Append(TEXT_GET_LINELENGTH, _T("Get the length of a line of the tabbed multiline"));
499 menu_bar->Append(menuText, _T("Te&xt"));
500
501 #if wxUSE_LOG
502 wxMenu *menuLog = new wxMenu;
503 menuLog->AppendCheckItem(TEXT_LOG_KEY, _T("Log &key events"));
504 menuLog->AppendCheckItem(TEXT_LOG_CHAR, _T("Log &char events"));
505 menuLog->AppendCheckItem(TEXT_LOG_MOUSE, _T("Log &mouse events"));
506 menuLog->AppendCheckItem(TEXT_LOG_TEXT, _T("Log &text events"));
507 menuLog->AppendCheckItem(TEXT_LOG_FOCUS, _T("Log &focus events"));
508 menuLog->AppendCheckItem(TEXT_LOG_CLIP, _T("Log clip&board events"));
509 menuLog->AppendSeparator();
510 menuLog->Append(TEXT_CLEAR, _T("&Clear the log\tCtrl-L"),
511 _T("Clear the log window contents"));
512
513 // select only the interesting events by default
514 MyTextCtrl::ms_logClip =
515 MyTextCtrl::ms_logText = true;
516
517 menuLog->Check(TEXT_LOG_KEY, MyTextCtrl::ms_logKey);
518 menuLog->Check(TEXT_LOG_CHAR, MyTextCtrl::ms_logChar);
519 menuLog->Check(TEXT_LOG_TEXT, MyTextCtrl::ms_logText);
520
521 menu_bar->Append(menuLog, _T("&Log"));
522 #endif // wxUSE_LOG
523
524 frame->SetMenuBar(menu_bar);
525
526 frame->Show(true);
527
528 SetTopWindow(frame);
529
530 // report success
531 return true;
532 }
533
534 //----------------------------------------------------------------------
535 // MyTextCtrl
536 //----------------------------------------------------------------------
537
538 BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
539 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
540 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
541 EVT_CHAR(MyTextCtrl::OnChar)
542
543 EVT_TEXT(wxID_ANY, MyTextCtrl::OnText)
544 EVT_TEXT_ENTER(wxID_ANY, MyTextCtrl::OnTextEnter)
545 EVT_TEXT_URL(wxID_ANY, MyTextCtrl::OnTextURL)
546 EVT_TEXT_MAXLEN(wxID_ANY, MyTextCtrl::OnTextMaxLen)
547 EVT_TEXT_CUT(wxID_ANY, MyTextCtrl::OnTextCut)
548 EVT_TEXT_COPY(wxID_ANY, MyTextCtrl::OnTextCopy)
549 EVT_TEXT_PASTE(wxID_ANY, MyTextCtrl::OnTextPaste)
550
551 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent)
552
553 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus)
554 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus)
555 END_EVENT_TABLE()
556
557 bool MyTextCtrl::ms_logKey = false;
558 bool MyTextCtrl::ms_logChar = false;
559 bool MyTextCtrl::ms_logMouse = false;
560 bool MyTextCtrl::ms_logText = false;
561 bool MyTextCtrl::ms_logFocus = false;
562 bool MyTextCtrl::ms_logClip = false;
563
564 void MyTextCtrl::LogKeyEvent(const wxChar *name, wxKeyEvent& event) const
565 {
566 wxString key;
567 long keycode = event.GetKeyCode();
568 {
569 switch ( keycode )
570 {
571 case WXK_BACK: key = _T("BACK"); break;
572 case WXK_TAB: key = _T("TAB"); break;
573 case WXK_RETURN: key = _T("RETURN"); break;
574 case WXK_ESCAPE: key = _T("ESCAPE"); break;
575 case WXK_SPACE: key = _T("SPACE"); break;
576 case WXK_DELETE: key = _T("DELETE"); break;
577 case WXK_START: key = _T("START"); break;
578 case WXK_LBUTTON: key = _T("LBUTTON"); break;
579 case WXK_RBUTTON: key = _T("RBUTTON"); break;
580 case WXK_CANCEL: key = _T("CANCEL"); break;
581 case WXK_MBUTTON: key = _T("MBUTTON"); break;
582 case WXK_CLEAR: key = _T("CLEAR"); break;
583 case WXK_SHIFT: key = _T("SHIFT"); break;
584 case WXK_ALT: key = _T("ALT"); break;
585 case WXK_CONTROL: key = _T("CONTROL"); break;
586 case WXK_MENU: key = _T("MENU"); break;
587 case WXK_PAUSE: key = _T("PAUSE"); break;
588 case WXK_CAPITAL: key = _T("CAPITAL"); break;
589 case WXK_END: key = _T("END"); break;
590 case WXK_HOME: key = _T("HOME"); break;
591 case WXK_LEFT: key = _T("LEFT"); break;
592 case WXK_UP: key = _T("UP"); break;
593 case WXK_RIGHT: key = _T("RIGHT"); break;
594 case WXK_DOWN: key = _T("DOWN"); break;
595 case WXK_SELECT: key = _T("SELECT"); break;
596 case WXK_PRINT: key = _T("PRINT"); break;
597 case WXK_EXECUTE: key = _T("EXECUTE"); break;
598 case WXK_SNAPSHOT: key = _T("SNAPSHOT"); break;
599 case WXK_INSERT: key = _T("INSERT"); break;
600 case WXK_HELP: key = _T("HELP"); break;
601 case WXK_NUMPAD0: key = _T("NUMPAD0"); break;
602 case WXK_NUMPAD1: key = _T("NUMPAD1"); break;
603 case WXK_NUMPAD2: key = _T("NUMPAD2"); break;
604 case WXK_NUMPAD3: key = _T("NUMPAD3"); break;
605 case WXK_NUMPAD4: key = _T("NUMPAD4"); break;
606 case WXK_NUMPAD5: key = _T("NUMPAD5"); break;
607 case WXK_NUMPAD6: key = _T("NUMPAD6"); break;
608 case WXK_NUMPAD7: key = _T("NUMPAD7"); break;
609 case WXK_NUMPAD8: key = _T("NUMPAD8"); break;
610 case WXK_NUMPAD9: key = _T("NUMPAD9"); break;
611 case WXK_MULTIPLY: key = _T("MULTIPLY"); break;
612 case WXK_ADD: key = _T("ADD"); break;
613 case WXK_SEPARATOR: key = _T("SEPARATOR"); break;
614 case WXK_SUBTRACT: key = _T("SUBTRACT"); break;
615 case WXK_DECIMAL: key = _T("DECIMAL"); break;
616 case WXK_DIVIDE: key = _T("DIVIDE"); break;
617 case WXK_F1: key = _T("F1"); break;
618 case WXK_F2: key = _T("F2"); break;
619 case WXK_F3: key = _T("F3"); break;
620 case WXK_F4: key = _T("F4"); break;
621 case WXK_F5: key = _T("F5"); break;
622 case WXK_F6: key = _T("F6"); break;
623 case WXK_F7: key = _T("F7"); break;
624 case WXK_F8: key = _T("F8"); break;
625 case WXK_F9: key = _T("F9"); break;
626 case WXK_F10: key = _T("F10"); break;
627 case WXK_F11: key = _T("F11"); break;
628 case WXK_F12: key = _T("F12"); break;
629 case WXK_F13: key = _T("F13"); break;
630 case WXK_F14: key = _T("F14"); break;
631 case WXK_F15: key = _T("F15"); break;
632 case WXK_F16: key = _T("F16"); break;
633 case WXK_F17: key = _T("F17"); break;
634 case WXK_F18: key = _T("F18"); break;
635 case WXK_F19: key = _T("F19"); break;
636 case WXK_F20: key = _T("F20"); break;
637 case WXK_F21: key = _T("F21"); break;
638 case WXK_F22: key = _T("F22"); break;
639 case WXK_F23: key = _T("F23"); break;
640 case WXK_F24: key = _T("F24"); break;
641 case WXK_NUMLOCK: key = _T("NUMLOCK"); break;
642 case WXK_SCROLL: key = _T("SCROLL"); break;
643 case WXK_PAGEUP: key = _T("PAGEUP"); break;
644 case WXK_PAGEDOWN: key = _T("PAGEDOWN"); break;
645 case WXK_NUMPAD_SPACE: key = _T("NUMPAD_SPACE"); break;
646 case WXK_NUMPAD_TAB: key = _T("NUMPAD_TAB"); break;
647 case WXK_NUMPAD_ENTER: key = _T("NUMPAD_ENTER"); break;
648 case WXK_NUMPAD_F1: key = _T("NUMPAD_F1"); break;
649 case WXK_NUMPAD_F2: key = _T("NUMPAD_F2"); break;
650 case WXK_NUMPAD_F3: key = _T("NUMPAD_F3"); break;
651 case WXK_NUMPAD_F4: key = _T("NUMPAD_F4"); break;
652 case WXK_NUMPAD_HOME: key = _T("NUMPAD_HOME"); break;
653 case WXK_NUMPAD_LEFT: key = _T("NUMPAD_LEFT"); break;
654 case WXK_NUMPAD_UP: key = _T("NUMPAD_UP"); break;
655 case WXK_NUMPAD_RIGHT: key = _T("NUMPAD_RIGHT"); break;
656 case WXK_NUMPAD_DOWN: key = _T("NUMPAD_DOWN"); break;
657 case WXK_NUMPAD_PAGEUP: key = _T("NUMPAD_PAGEUP"); break;
658 case WXK_NUMPAD_PAGEDOWN: key = _T("NUMPAD_PAGEDOWN"); break;
659 case WXK_NUMPAD_END: key = _T("NUMPAD_END"); break;
660 case WXK_NUMPAD_BEGIN: key = _T("NUMPAD_BEGIN"); break;
661 case WXK_NUMPAD_INSERT: key = _T("NUMPAD_INSERT"); break;
662 case WXK_NUMPAD_DELETE: key = _T("NUMPAD_DELETE"); break;
663 case WXK_NUMPAD_EQUAL: key = _T("NUMPAD_EQUAL"); break;
664 case WXK_NUMPAD_MULTIPLY: key = _T("NUMPAD_MULTIPLY"); break;
665 case WXK_NUMPAD_ADD: key = _T("NUMPAD_ADD"); break;
666 case WXK_NUMPAD_SEPARATOR: key = _T("NUMPAD_SEPARATOR"); break;
667 case WXK_NUMPAD_SUBTRACT: key = _T("NUMPAD_SUBTRACT"); break;
668 case WXK_NUMPAD_DECIMAL: key = _T("NUMPAD_DECIMAL"); break;
669
670 default:
671 {
672 if ( wxIsprint((int)keycode) )
673 key.Printf(_T("'%c'"), (char)keycode);
674 else if ( keycode > 0 && keycode < 27 )
675 key.Printf(_("Ctrl-%c"), _T('A') + keycode - 1);
676 else
677 key.Printf(_T("unknown (%ld)"), keycode);
678 }
679 }
680 }
681
682 #if wxUSE_UNICODE
683 key += wxString::Format(_T(" (Unicode: %#04x)"), event.GetUnicodeKey());
684 #endif // wxUSE_UNICODE
685
686 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
687 name,
688 key.c_str(),
689 GetChar( event.ControlDown(), _T('C') ),
690 GetChar( event.AltDown(), _T('A') ),
691 GetChar( event.ShiftDown(), _T('S') ),
692 GetChar( event.MetaDown(), _T('M') ) );
693 }
694
695 static wxString GetMouseEventDesc(const wxMouseEvent& ev)
696 {
697 // click event
698 wxString button;
699 bool dbl, up;
700 if ( ev.LeftDown() || ev.LeftUp() || ev.LeftDClick() )
701 {
702 button = _T("Left");
703 dbl = ev.LeftDClick();
704 up = ev.LeftUp();
705 }
706 else if ( ev.MiddleDown() || ev.MiddleUp() || ev.MiddleDClick() )
707 {
708 button = _T("Middle");
709 dbl = ev.MiddleDClick();
710 up = ev.MiddleUp();
711 }
712 else if ( ev.RightDown() || ev.RightUp() || ev.RightDClick() )
713 {
714 button = _T("Right");
715 dbl = ev.RightDClick();
716 up = ev.RightUp();
717 }
718 else
719 {
720 return _T("Unknown mouse event");
721 }
722
723 return wxString::Format(_T("%s mouse button %s"),
724 button.c_str(),
725 dbl ? _T("double clicked")
726 : up ? _T("released") : _T("clicked"));
727 }
728
729 void MyTextCtrl::OnMouseEvent(wxMouseEvent& ev)
730 {
731 ev.Skip();
732
733 if ( !ms_logMouse )
734 return;
735
736 if ( !ev.Moving() )
737 {
738 wxString msg;
739 if ( ev.Entering() )
740 {
741 msg = _T("Mouse entered the window");
742 }
743 else if ( ev.Leaving() )
744 {
745 msg = _T("Mouse left the window");
746 }
747 else
748 {
749 msg = GetMouseEventDesc(ev);
750 }
751
752 msg << _T(" at (") << ev.GetX() << _T(", ") << ev.GetY() << _T(") ");
753
754 long pos;
755 wxTextCtrlHitTestResult rc = HitTest(ev.GetPosition(), &pos);
756 if ( rc != wxTE_HT_UNKNOWN )
757 {
758 msg << _T("at position ") << pos << _T(' ');
759 }
760
761 msg << _T("[Flags: ")
762 << GetChar( ev.LeftIsDown(), _T('1') )
763 << GetChar( ev.MiddleIsDown(), _T('2') )
764 << GetChar( ev.RightIsDown(), _T('3') )
765 << GetChar( ev.ControlDown(), _T('C') )
766 << GetChar( ev.AltDown(), _T('A') )
767 << GetChar( ev.ShiftDown(), _T('S') )
768 << GetChar( ev.MetaDown(), _T('M') )
769 << _T(']');
770
771 wxLogMessage(msg);
772 }
773 //else: we're not interested in mouse move events
774 }
775
776 void MyTextCtrl::OnSetFocus(wxFocusEvent& event)
777 {
778 if ( ms_logFocus )
779 wxLogMessage( wxT("%p got focus."), this);
780
781 event.Skip();
782 }
783
784 void MyTextCtrl::OnKillFocus(wxFocusEvent& event)
785 {
786 if ( ms_logFocus )
787 wxLogMessage( wxT("%p lost focus"), this);
788
789 event.Skip();
790 }
791
792 void MyTextCtrl::OnText(wxCommandEvent& event)
793 {
794 if ( !ms_logText )
795 return;
796
797 MyTextCtrl *win = (MyTextCtrl *)event.GetEventObject();
798 const wxChar *changeVerb = win->IsModified() ? _T("changed")
799 : _T("set by program");
800 const wxChar *data = (const wxChar *)(win->GetClientData());
801 if ( data )
802 {
803 wxLogMessage(_T("Text %s in control \"%s\""), changeVerb, data);
804 }
805 else
806 {
807 wxLogMessage(_T("Text %s in some control"), changeVerb);
808 }
809 }
810
811 void MyTextCtrl::OnTextEnter(wxCommandEvent& event)
812 {
813 if ( !ms_logText )
814 return;
815
816 MyTextCtrl *win = (MyTextCtrl *)event.GetEventObject();
817 const wxChar *data = (const wxChar *)(win->GetClientData());
818 if ( data )
819 {
820 wxLogMessage(_T("Enter pressed in control '%s'"), data);
821 }
822 else
823 {
824 wxLogMessage(_T("Enter pressed in some control"));
825 }
826 }
827
828 void MyTextCtrl::OnTextMaxLen(wxCommandEvent& WXUNUSED(event))
829 {
830 wxLogMessage(_T("You can't enter more characters into this control."));
831 }
832
833
834 void MyTextCtrl::OnTextCut(wxClipboardTextEvent& event)
835 {
836 LogClipEvent(_T("cut to"), event);
837 }
838
839 void MyTextCtrl::OnTextCopy(wxClipboardTextEvent& event)
840 {
841 LogClipEvent(_T("copied to"), event);
842 }
843
844 void MyTextCtrl::OnTextPaste(wxClipboardTextEvent& event)
845 {
846 LogClipEvent(_T("pasted from"), event);
847 }
848
849 void MyTextCtrl::LogClipEvent(const wxChar *what, wxClipboardTextEvent& event)
850 {
851 wxFrame *frame = wxDynamicCast(wxGetTopLevelParent(this), wxFrame);
852 wxCHECK_RET( frame, _T("no parent frame?") );
853
854 const bool veto = frame->GetMenuBar()->IsChecked(TEXT_CLIPBOARD_VETO);
855 if ( !veto )
856 event.Skip();
857
858 if ( ms_logClip )
859 {
860 wxLogMessage(_T("Text %s%s the clipboard."),
861 veto ? _T("not ") : _T(""), what);
862 }
863 }
864
865
866 void MyTextCtrl::OnTextURL(wxTextUrlEvent& event)
867 {
868 const wxMouseEvent& ev = event.GetMouseEvent();
869
870 // filter out mouse moves, too many of them
871 if ( ev.Moving() )
872 return;
873
874 long start = event.GetURLStart(),
875 end = event.GetURLEnd();
876
877 wxLogMessage(_T("Mouse event over URL '%s': %s"),
878 GetValue().Mid(start, end - start).c_str(),
879 GetMouseEventDesc(ev).c_str());
880 }
881
882 void MyTextCtrl::OnChar(wxKeyEvent& event)
883 {
884 if ( ms_logChar )
885 LogKeyEvent( _T("Char"), event);
886
887 event.Skip();
888 }
889
890 void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
891 {
892 if ( ms_logKey )
893 LogKeyEvent( _T("Key up"), event);
894
895 event.Skip();
896 }
897
898 void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
899 {
900 switch ( event.GetKeyCode() )
901 {
902 case WXK_F1:
903 // show current position and text length
904 {
905 long line, column, pos = GetInsertionPoint();
906 PositionToXY(pos, &column, &line);
907
908 wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
909 pos,
910 line, column,
911 (long) GetNumberOfLines(),
912 (long) GetLineLength(line),
913 (unsigned int) GetValue().length(),
914 GetLastPosition());
915
916 long from, to;
917 GetSelection(&from, &to);
918
919 wxString sel = GetStringSelection();
920
921 wxLogMessage(_T("Selection: from %ld to %ld."), from, to);
922 wxLogMessage(_T("Selection = '%s' (len = %u)"),
923 sel.c_str(),
924 (unsigned int) sel.length());
925
926 const wxString text = GetLineText(line);
927 wxLogMessage(_T("Current line: \"%s\"; length = %lu"),
928 text.c_str(), text.length());
929 }
930 break;
931
932 case WXK_F2:
933 // go to the end
934 SetInsertionPointEnd();
935 break;
936
937 case WXK_F3:
938 // go to position 10
939 SetInsertionPoint(10);
940 break;
941
942 case WXK_F4:
943 if (!m_hasCapture)
944 {
945 wxLogDebug( wxT("Now capturing mouse and events.") );
946 m_hasCapture = true;
947 CaptureMouse();
948 }
949 else
950 {
951 wxLogDebug( wxT("Stopped capturing mouse and events.") );
952 m_hasCapture = false;
953 ReleaseMouse();
954 }
955 break;
956
957 case WXK_F5:
958 // insert a blank line
959 WriteText(_T("\n"));
960 break;
961
962 case WXK_F6:
963 wxLogMessage(_T("IsModified() before SetValue(): %d"),
964 IsModified());
965 ChangeValue(_T("ChangeValue() has been called"));
966 wxLogMessage(_T("IsModified() after SetValue(): %d"),
967 IsModified());
968 break;
969
970 case WXK_F7:
971 wxLogMessage(_T("Position 10 should be now visible."));
972 ShowPosition(10);
973 break;
974
975 case WXK_F8:
976 wxLogMessage(_T("Control has been cleared"));
977 Clear();
978 break;
979
980 case WXK_F9:
981 WriteText(_T("WriteText() has been called"));
982 break;
983
984 case WXK_F10:
985 AppendText(_T("AppendText() has been called"));
986 break;
987
988 case WXK_F11:
989 DiscardEdits();
990 wxLogMessage(_T("Control marked as non modified"));
991 break;
992 }
993
994 if ( ms_logKey )
995 LogKeyEvent( wxT("Key down"), event);
996
997 event.Skip();
998 }
999
1000 //----------------------------------------------------------------------
1001 // MyPanel
1002 //----------------------------------------------------------------------
1003
1004 MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
1005 : wxPanel( frame, wxID_ANY, wxPoint(x, y), wxSize(w, h) )
1006 {
1007 #if wxUSE_LOG
1008 m_log = new wxTextCtrl( this, wxID_ANY, _T("This is the log window.\n"),
1009 wxPoint(5,260), wxSize(630,100),
1010 wxTE_MULTILINE | wxTE_READONLY /* | wxTE_RICH */);
1011
1012 m_logOld = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
1013 #endif // wxUSE_LOG
1014
1015 // single line text controls
1016
1017 m_text = new MyTextCtrl( this, wxID_ANY, _T("Single line."),
1018 wxDefaultPosition, wxDefaultSize,
1019 wxTE_PROCESS_ENTER);
1020 m_text->SetForegroundColour(*wxBLUE);
1021 m_text->SetBackgroundColour(*wxLIGHT_GREY);
1022 (*m_text) << _T(" Appended.");
1023 m_text->SetInsertionPoint(0);
1024 m_text->WriteText( _T("Prepended. ") );
1025
1026 m_password = new MyTextCtrl( this, wxID_ANY, _T(""),
1027 wxPoint(10,50), wxSize(140,wxDefaultCoord), wxTE_PASSWORD );
1028
1029 m_readonly = new MyTextCtrl( this, wxID_ANY, _T("Read only"),
1030 wxPoint(10,90), wxSize(140,wxDefaultCoord), wxTE_READONLY );
1031
1032 m_limited = new MyTextCtrl(this, wxID_ANY, _T("Max 8 ch"),
1033 wxPoint(10, 130), wxSize(140, wxDefaultCoord));
1034 m_limited->SetMaxLength(8);
1035
1036 // multi line text controls
1037
1038 m_horizontal = new MyTextCtrl( this, wxID_ANY, _T("Multiline text control with a horizontal scrollbar.\n"),
1039 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL);
1040
1041 // a little hack to use the command line argument for encoding testing
1042 if ( wxTheApp->argc == 2 )
1043 {
1044 switch ( (wxChar)wxTheApp->argv[1][0] )
1045 {
1046 case '2':
1047 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
1048 false, _T(""),
1049 wxFONTENCODING_ISO8859_2));
1050 m_horizontal->AppendText(_T("\256lu\273ou\350k\375 k\371\362 zb\354sile \350e\271tina \253\273"));
1051 break;
1052
1053 case '1':
1054 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
1055 false, _T(""),
1056 wxFONTENCODING_CP1251));
1057 m_horizontal->AppendText(_T("\317\360\350\342\345\362!"));
1058 break;
1059
1060 case '8':
1061 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
1062 false, _T(""),
1063 wxFONTENCODING_CP1251));
1064 #if wxUSE_UNICODE
1065 m_horizontal->AppendText(L"\x0412\x0430\x0434\x0438\x043c \x0426");
1066 #else
1067 m_horizontal->AppendText("\313\301\326\305\324\323\321 \325\304\301\336\316\331\315");
1068 #endif
1069 }
1070 }
1071 else
1072 {
1073 m_horizontal->AppendText(_T("Text in default encoding"));
1074 }
1075
1076 m_multitext = new MyTextCtrl( this, wxID_ANY,
1077 _T("Multi line without vertical scrollbar."),
1078 wxPoint(180,10), wxSize(200,70), wxTE_MULTILINE | wxTE_NO_VSCROLL );
1079 m_multitext->SetFont(*wxITALIC_FONT);
1080 (*m_multitext) << _T(" Appended.");
1081 m_multitext->SetInsertionPoint(0);
1082 m_multitext->WriteText( _T("Prepended. ") );
1083 m_multitext->SetForegroundColour(*wxRED);
1084 m_multitext->SetBackgroundColour(*wxLIGHT_GREY);
1085
1086 #if wxUSE_TOOLTIPS
1087 m_multitext->SetToolTip(_T("Press Fn function keys here"));
1088 #endif
1089
1090 m_tab = new MyTextCtrl( this, 100, _T("Multiline, allow <TAB> processing."),
1091 wxPoint(180,90), wxSize(200,70), wxTE_MULTILINE | wxTE_PROCESS_TAB );
1092 m_tab->SetClientData((void *)_T("tab"));
1093
1094 m_enter = new MyTextCtrl( this, 100, _T("Multiline, allow <ENTER> processing."),
1095 wxPoint(180,170), wxSize(200,70), wxTE_MULTILINE);
1096 m_enter->SetClientData((void *)_T("enter"));
1097
1098 m_textrich = new MyTextCtrl(this, wxID_ANY, _T("Allows more than 30Kb of text\n")
1099 _T("(even under broken Win9x)\n")
1100 _T("and a very very very very very ")
1101 _T("very very very long line to test ")
1102 _T("wxHSCROLL style\n")
1103 _T("\nAnd here is a link in quotation marks to ")
1104 _T("test wxTE_AUTO_URL: \"http://www.wxwidgets.org\""),
1105 wxPoint(450, 10), wxSize(200, 230),
1106 wxTE_RICH | wxTE_MULTILINE | wxTE_AUTO_URL);
1107 m_textrich->SetStyle(0, 10, *wxRED);
1108 m_textrich->SetStyle(10, 20, *wxBLUE);
1109 m_textrich->SetStyle(30, 40,
1110 wxTextAttr(*wxGREEN, wxNullColour, *wxITALIC_FONT));
1111 m_textrich->SetDefaultStyle(wxTextAttr());
1112 m_textrich->AppendText(_T("\n\nFirst 10 characters should be in red\n"));
1113 m_textrich->AppendText(_T("Next 10 characters should be in blue\n"));
1114 m_textrich->AppendText(_T("Next 10 characters should be normal\n"));
1115 m_textrich->AppendText(_T("And the next 10 characters should be green and italic\n"));
1116 m_textrich->SetDefaultStyle(wxTextAttr(*wxCYAN, *wxBLUE));
1117 m_textrich->AppendText(_T("This text should be cyan on blue\n"));
1118 m_textrich->SetDefaultStyle(wxTextAttr(*wxBLUE, *wxWHITE));
1119 m_textrich->AppendText(_T("And this should be in blue and the text you ")
1120 _T("type should be in blue as well"));
1121
1122
1123 // lay out the controls
1124 wxBoxSizer *column1 = new wxBoxSizer(wxVERTICAL);
1125 column1->Add( m_text, 0, wxALL | wxEXPAND, 10 );
1126 column1->Add( m_password, 0, wxALL | wxEXPAND, 10 );
1127 column1->Add( m_readonly, 0, wxALL | wxEXPAND, 10 );
1128 column1->Add( m_limited, 0, wxALL | wxEXPAND, 10 );
1129 column1->Add( m_horizontal, 1, wxALL | wxEXPAND, 10 );
1130
1131 wxBoxSizer *column2 = new wxBoxSizer(wxVERTICAL);
1132 column2->Add( m_multitext, 1, wxALL | wxEXPAND, 10 );
1133 column2->Add( m_tab, 1, wxALL | wxEXPAND, 10 );
1134 column2->Add( m_enter, 1, wxALL | wxEXPAND, 10 );
1135
1136 wxBoxSizer *row1 = new wxBoxSizer(wxHORIZONTAL);
1137 row1->Add( column1, 0, wxALL | wxEXPAND, 10 );
1138 row1->Add( column2, 1, wxALL | wxEXPAND, 10 );
1139 row1->Add( m_textrich, 1, wxALL | wxEXPAND, 10 );
1140
1141 wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
1142 topSizer->Add( row1, 2, wxALL | wxEXPAND, 10 );
1143
1144 #if wxUSE_LOG
1145 topSizer->Add( m_log, 1, wxALL | wxEXPAND, 10 );
1146 #endif
1147
1148 SetAutoLayout( true );
1149 SetSizer(topSizer);
1150 }
1151
1152 wxTextCtrl *MyPanel::GetFocusedText() const
1153 {
1154 wxWindow *win = FindFocus();
1155
1156 wxTextCtrl *text = win ? wxDynamicCast(win, wxTextCtrl) : NULL;
1157 return text ? text : m_multitext;
1158 }
1159
1160 #if wxUSE_CLIPBOARD
1161 void MyPanel::DoPasteFromClipboard()
1162 {
1163 // On X11, we want to get the data from the primary selection instead
1164 // of the normal clipboard (which isn't normal under X11 at all). This
1165 // call has no effect under MSW.
1166 wxTheClipboard->UsePrimarySelection();
1167
1168 if (!wxTheClipboard->Open())
1169 {
1170 #if wxUSE_LOG
1171 *m_log << _T("Error opening the clipboard.\n");
1172 #endif // wxUSE_LOG
1173 return;
1174 }
1175 else
1176 {
1177 #if wxUSE_LOG
1178 *m_log << _T("Successfully opened the clipboard.\n");
1179 #endif // wxUSE_LOG
1180 }
1181
1182 wxTextDataObject data;
1183
1184 if (wxTheClipboard->IsSupported( data.GetFormat() ))
1185 {
1186 #if wxUSE_LOG
1187 *m_log << _T("Clipboard supports requested format.\n");
1188 #endif // wxUSE_LOG
1189
1190 if (wxTheClipboard->GetData( data ))
1191 {
1192 #if wxUSE_LOG
1193 *m_log << _T("Successfully retrieved data from the clipboard.\n");
1194 #endif // wxUSE_LOG
1195 GetFocusedText()->AppendText(data.GetText());
1196 }
1197 else
1198 {
1199 #if wxUSE_LOG
1200 *m_log << _T("Error getting data from the clipboard.\n");
1201 #endif // wxUSE_LOG
1202 }
1203 }
1204 else
1205 {
1206 #if wxUSE_LOG
1207 *m_log << _T("Clipboard doesn't support requested format.\n");
1208 #endif // wxUSE_LOG
1209 }
1210
1211 wxTheClipboard->Close();
1212
1213 #if wxUSE_LOG
1214 *m_log << _T("Closed the clipboard.\n");
1215 #endif // wxUSE_LOG
1216 }
1217
1218 void MyPanel::DoCopyToClipboard()
1219 {
1220 // On X11, we want to get the data from the primary selection instead
1221 // of the normal clipboard (which isn't normal under X11 at all). This
1222 // call has no effect under MSW.
1223 wxTheClipboard->UsePrimarySelection();
1224
1225 wxString text( GetFocusedText()->GetStringSelection() );
1226
1227 if (text.IsEmpty())
1228 {
1229 #if wxUSE_LOG
1230 *m_log << _T("No text to copy.\n");
1231 #endif // wxUSE_LOG
1232
1233 return;
1234 }
1235
1236 if (!wxTheClipboard->Open())
1237 {
1238 #if wxUSE_LOG
1239 *m_log << _T("Error opening the clipboard.\n");
1240 #endif // wxUSE_LOG
1241
1242 return;
1243 }
1244 else
1245 {
1246 #if wxUSE_LOG
1247 *m_log << _T("Successfully opened the clipboard.\n");
1248 #endif // wxUSE_LOG
1249 }
1250
1251 wxTextDataObject *data = new wxTextDataObject( text );
1252
1253 if (!wxTheClipboard->SetData( data ))
1254 {
1255 #if wxUSE_LOG
1256 *m_log << _T("Error while copying to the clipboard.\n");
1257 #endif // wxUSE_LOG
1258 }
1259 else
1260 {
1261 #if wxUSE_LOG
1262 *m_log << _T("Successfully copied data to the clipboard.\n");
1263 #endif // wxUSE_LOG
1264 }
1265
1266 wxTheClipboard->Close();
1267
1268 #if wxUSE_LOG
1269 *m_log << _T("Closed the clipboard.\n");
1270 #endif // wxUSE_LOG
1271 }
1272
1273 #endif // wxUSE_CLIPBOARD
1274
1275 void MyPanel::DoMoveToEndOfText()
1276 {
1277 m_multitext->SetInsertionPointEnd();
1278 m_multitext->SetFocus();
1279 }
1280
1281 void MyPanel::DoMoveToEndOfEntry()
1282 {
1283 m_text->SetInsertionPointEnd();
1284 m_text->SetFocus();
1285 }
1286
1287 void MyPanel::DoRemoveText()
1288 {
1289 GetFocusedText()->Remove(0, 10);
1290 }
1291
1292 void MyPanel::DoReplaceText()
1293 {
1294 GetFocusedText()->Replace(3, 8, _T("ABC"));
1295 }
1296
1297 void MyPanel::DoSelectText()
1298 {
1299 GetFocusedText()->SetSelection(3, 8);
1300 }
1301
1302 //----------------------------------------------------------------------
1303 // MyFrame
1304 //----------------------------------------------------------------------
1305
1306 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
1307 EVT_MENU(TEXT_QUIT, MyFrame::OnQuit)
1308 EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout)
1309 EVT_MENU(TEXT_SAVE, MyFrame::OnFileSave)
1310 EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad)
1311 EVT_MENU(TEXT_RICH_TEXT_TEST, MyFrame::OnRichTextTest)
1312
1313 EVT_MENU(TEXT_LOG_KEY, MyFrame::OnLogKey)
1314 EVT_MENU(TEXT_LOG_CHAR, MyFrame::OnLogChar)
1315 EVT_MENU(TEXT_LOG_MOUSE,MyFrame::OnLogMouse)
1316 EVT_MENU(TEXT_LOG_TEXT, MyFrame::OnLogText)
1317 EVT_MENU(TEXT_LOG_FOCUS,MyFrame::OnLogFocus)
1318 EVT_MENU(TEXT_LOG_CLIP, MyFrame::OnLogClip)
1319 #if wxUSE_LOG
1320 EVT_MENU(TEXT_CLEAR, MyFrame::OnLogClear)
1321 #endif // wxUSE_LOG
1322
1323 #if wxUSE_TOOLTIPS
1324 EVT_MENU(TEXT_TOOLTIPS_SETDELAY, MyFrame::OnSetTooltipDelay)
1325 EVT_MENU(TEXT_TOOLTIPS_ENABLE, MyFrame::OnToggleTooltips)
1326 #endif // wxUSE_TOOLTIPS
1327
1328 #if wxUSE_CLIPBOARD
1329 EVT_MENU(TEXT_CLIPBOARD_PASTE, MyFrame::OnPasteFromClipboard)
1330 EVT_MENU(TEXT_CLIPBOARD_COPY, MyFrame::OnCopyToClipboard)
1331
1332 EVT_UPDATE_UI(TEXT_CLIPBOARD_PASTE, MyFrame::OnUpdatePasteFromClipboard)
1333 EVT_UPDATE_UI(TEXT_CLIPBOARD_COPY, MyFrame::OnUpdateCopyToClipboard)
1334 #endif // wxUSE_CLIPBOARD
1335
1336 EVT_MENU(TEXT_REMOVE, MyFrame::OnRemoveText)
1337 EVT_MENU(TEXT_REPLACE, MyFrame::OnReplaceText)
1338 EVT_MENU(TEXT_SELECT, MyFrame::OnSelectText)
1339 EVT_MENU(TEXT_ADD_SOME, MyFrame::OnAddText)
1340 EVT_MENU(TEXT_ADD_FREEZE, MyFrame::OnAddTextFreeze)
1341 EVT_MENU(TEXT_MOVE_ENDTEXT, MyFrame::OnMoveToEndOfText)
1342 EVT_MENU(TEXT_MOVE_ENDENTRY, MyFrame::OnMoveToEndOfEntry)
1343
1344 EVT_MENU(TEXT_SET_EDITABLE, MyFrame::OnSetEditable)
1345 EVT_MENU(TEXT_SET_ENABLED, MyFrame::OnSetEnabled)
1346
1347 EVT_MENU(TEXT_LINE_DOWN, MyFrame::OnScrollLineDown)
1348 EVT_MENU(TEXT_LINE_UP, MyFrame::OnScrollLineUp)
1349 EVT_MENU(TEXT_PAGE_DOWN, MyFrame::OnScrollPageDown)
1350 EVT_MENU(TEXT_PAGE_UP, MyFrame::OnScrollPageUp)
1351
1352 EVT_MENU(TEXT_GET_LINE, MyFrame::OnGetLine)
1353 EVT_MENU(TEXT_GET_LINELENGTH, MyFrame::OnGetLineLength)
1354
1355 EVT_MENU(TEXT_SET, MyFrame::OnSetText)
1356 EVT_MENU(TEXT_CHANGE, MyFrame::OnChangeText)
1357
1358 EVT_IDLE(MyFrame::OnIdle)
1359 END_EVENT_TABLE()
1360
1361 MyFrame::MyFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h)
1362 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h) )
1363 {
1364 #if wxUSE_STATUSBAR
1365 CreateStatusBar(2);
1366 #endif // wxUSE_STATUSBAR
1367
1368 m_panel = new MyPanel( this, 10, 10, 300, 100 );
1369 }
1370
1371 void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) )
1372 {
1373 Close(true);
1374 }
1375
1376 void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) )
1377 {
1378 wxBeginBusyCursor();
1379
1380 wxMessageDialog dialog(this,
1381 _T("This is a text control sample. It demonstrates the many different\n")
1382 _T("text control styles, the use of the clipboard, setting and handling\n")
1383 _T("tooltips and intercepting key and char events.\n")
1384 _T("\n")
1385 _T("Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin"),
1386 _T("About wxTextCtrl Sample"),
1387 wxOK | wxICON_INFORMATION);
1388
1389 dialog.ShowModal();
1390
1391 wxEndBusyCursor();
1392 }
1393
1394 #if wxUSE_TOOLTIPS
1395
1396 void MyFrame::OnSetTooltipDelay(wxCommandEvent& WXUNUSED(event))
1397 {
1398 static long s_delay = 5000;
1399
1400 wxString delay;
1401 delay.Printf( _T("%ld"), s_delay);
1402
1403 delay = wxGetTextFromUser(_T("Enter delay (in milliseconds)"),
1404 _T("Set tooltip delay"),
1405 delay,
1406 this);
1407 if ( !delay )
1408 return; // cancelled
1409
1410 wxSscanf(delay, _T("%ld"), &s_delay);
1411
1412 wxToolTip::SetDelay(s_delay);
1413
1414 wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay);
1415 }
1416
1417 void MyFrame::OnToggleTooltips(wxCommandEvent& WXUNUSED(event))
1418 {
1419 static bool s_enabled = true;
1420
1421 s_enabled = !s_enabled;
1422
1423 wxToolTip::Enable(s_enabled);
1424
1425 wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") );
1426 }
1427 #endif // tooltips
1428
1429 #if wxUSE_LOG
1430 void MyFrame::OnLogClear(wxCommandEvent& WXUNUSED(event))
1431 {
1432 m_panel->m_log->Clear();
1433 }
1434 #endif // wxUSE_LOG
1435
1436 void MyFrame::OnSetEditable(wxCommandEvent& WXUNUSED(event))
1437 {
1438 static bool s_editable = true;
1439
1440 s_editable = !s_editable;
1441 m_panel->m_text->SetEditable(s_editable);
1442 m_panel->m_password->SetEditable(s_editable);
1443 m_panel->m_multitext->SetEditable(s_editable);
1444 m_panel->m_textrich->SetEditable(s_editable);
1445 }
1446
1447 void MyFrame::OnSetEnabled(wxCommandEvent& WXUNUSED(event))
1448 {
1449 bool enabled = m_panel->m_text->IsEnabled();
1450 enabled = !enabled;
1451
1452 m_panel->m_text->Enable(enabled);
1453 m_panel->m_password->Enable(enabled);
1454 m_panel->m_multitext->Enable(enabled);
1455 m_panel->m_readonly->Enable(enabled);
1456 m_panel->m_limited->Enable(enabled);
1457 m_panel->m_textrich->Enable(enabled);
1458 }
1459
1460 void MyFrame::OnFileSave(wxCommandEvent& WXUNUSED(event))
1461 {
1462 if ( m_panel->m_textrich->SaveFile(_T("dummy.txt")) )
1463 {
1464 #if wxUSE_FILE
1465 // verify that the fil length is correct (it wasn't under Win95)
1466 wxFile file(wxT("dummy.txt"));
1467 wxLogStatus(this,
1468 _T("Successfully saved file (text len = %lu, file size = %ld)"),
1469 (unsigned long)m_panel->m_textrich->GetValue().length(),
1470 (long) file.Length());
1471 #endif
1472 }
1473 else
1474 wxLogStatus(this, _T("Couldn't save the file"));
1475 }
1476
1477 void MyFrame::OnFileLoad(wxCommandEvent& WXUNUSED(event))
1478 {
1479 if ( m_panel->m_textrich->LoadFile(_T("dummy.txt")) )
1480 wxLogStatus(this, _T("Successfully loaded file"));
1481 else
1482 wxLogStatus(this, _T("Couldn't load the file"));
1483 }
1484
1485 void MyFrame::OnRichTextTest(wxCommandEvent& WXUNUSED(event))
1486 {
1487 RichTextFrame* frame = new RichTextFrame(this, _T("Rich Text Editor"));
1488 frame->Show(true);
1489 }
1490
1491 void MyFrame::OnIdle( wxIdleEvent& event )
1492 {
1493 // track the window which has the focus in the status bar
1494 static wxWindow *s_windowFocus = (wxWindow *)NULL;
1495 wxWindow *focus = wxWindow::FindFocus();
1496 if ( focus && (focus != s_windowFocus) )
1497 {
1498 s_windowFocus = focus;
1499
1500 wxString msg;
1501 msg.Printf(
1502 #ifdef __WXMSW__
1503 _T("Focus: wxWindow = %p, HWND = %08x"),
1504 #else
1505 _T("Focus: wxWindow = %p"),
1506 #endif
1507 s_windowFocus
1508 #ifdef __WXMSW__
1509 , (unsigned int) s_windowFocus->GetHWND()
1510 #endif
1511 );
1512
1513 #if wxUSE_STATUSBAR
1514 SetStatusText(msg);
1515 #endif // wxUSE_STATUSBAR
1516 }
1517 event.Skip();
1518 }
1519
1520 /*
1521 * RichTextFrame is used to demonstrate rich text behaviour
1522 */
1523
1524 enum
1525 {
1526 RICHTEXT_CLOSE = 1000,
1527 RICHTEXT_LEFT_ALIGN,
1528 RICHTEXT_RIGHT_ALIGN,
1529 RICHTEXT_CENTRE,
1530 RICHTEXT_JUSTIFY,
1531 RICHTEXT_CHANGE_FONT,
1532 RICHTEXT_CHANGE_TEXT_COLOUR,
1533 RICHTEXT_CHANGE_BACKGROUND_COLOUR,
1534 RICHTEXT_LEFT_INDENT,
1535 RICHTEXT_RIGHT_INDENT,
1536 RICHTEXT_TAB_STOPS
1537 };
1538
1539 BEGIN_EVENT_TABLE(RichTextFrame, wxFrame)
1540 EVT_IDLE(RichTextFrame::OnIdle)
1541 EVT_MENU(RICHTEXT_CLOSE, RichTextFrame::OnClose)
1542 EVT_MENU(RICHTEXT_LEFT_ALIGN, RichTextFrame::OnLeftAlign)
1543 EVT_MENU(RICHTEXT_RIGHT_ALIGN, RichTextFrame::OnRightAlign)
1544 EVT_MENU(RICHTEXT_CENTRE, RichTextFrame::OnCentre)
1545 EVT_MENU(RICHTEXT_JUSTIFY, RichTextFrame::OnJustify)
1546 EVT_MENU(RICHTEXT_CHANGE_FONT, RichTextFrame::OnChangeFont)
1547 EVT_MENU(RICHTEXT_CHANGE_TEXT_COLOUR, RichTextFrame::OnChangeTextColour)
1548 EVT_MENU(RICHTEXT_CHANGE_BACKGROUND_COLOUR, RichTextFrame::OnChangeBackgroundColour)
1549 EVT_MENU(RICHTEXT_LEFT_INDENT, RichTextFrame::OnLeftIndent)
1550 EVT_MENU(RICHTEXT_RIGHT_INDENT, RichTextFrame::OnRightIndent)
1551 EVT_MENU(RICHTEXT_TAB_STOPS, RichTextFrame::OnTabStops)
1552 END_EVENT_TABLE()
1553
1554 RichTextFrame::RichTextFrame(wxWindow* parent, const wxString& title):
1555 wxFrame(parent, wxID_ANY, title, wxDefaultPosition, wxSize(300, 400))
1556 {
1557 m_currentPosition = -1;
1558 m_textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1559 wxDefaultSize, wxTE_MULTILINE|wxTE_RICH2);
1560
1561 wxString value;
1562 int i;
1563 for (i = 0; i < 10; i++)
1564 {
1565 int j;
1566 for (j = 0; j < 10; j++)
1567 {
1568 value << wxT("Hello, welcome to a very simple rich text editor. You can set some character and paragraph styles from the Edit menu. ");
1569 }
1570 value << wxT("\n\n");
1571 }
1572 m_textCtrl->SetValue(value);
1573
1574 wxMenuBar* menuBar = new wxMenuBar;
1575 wxMenu* fileMenu = new wxMenu;
1576 fileMenu->Append(RICHTEXT_CLOSE, _("Close\tCtrl+W"));
1577 menuBar->Append(fileMenu, _("File"));
1578
1579 wxMenu* editMenu = new wxMenu;
1580 editMenu->Append(RICHTEXT_LEFT_ALIGN, _("Left Align"));
1581 editMenu->Append(RICHTEXT_RIGHT_ALIGN, _("Right Align"));
1582 editMenu->Append(RICHTEXT_CENTRE, _("Centre"));
1583 editMenu->Append(RICHTEXT_JUSTIFY, _("Justify"));
1584 editMenu->AppendSeparator();
1585 editMenu->Append(RICHTEXT_CHANGE_FONT, _("Change Font"));
1586 editMenu->Append(RICHTEXT_CHANGE_TEXT_COLOUR, _("Change Text Colour"));
1587 editMenu->Append(RICHTEXT_CHANGE_BACKGROUND_COLOUR, _("Change Background Colour"));
1588 editMenu->AppendSeparator();
1589 editMenu->Append(RICHTEXT_LEFT_INDENT, _("Left Indent"));
1590 editMenu->Append(RICHTEXT_RIGHT_INDENT, _("Right Indent"));
1591 editMenu->Append(RICHTEXT_TAB_STOPS, _("Tab Stops"));
1592 menuBar->Append(editMenu, _("Edit"));
1593
1594 SetMenuBar(menuBar);
1595 #if wxUSE_STATUSBAR
1596 CreateStatusBar();
1597 #endif // wxUSE_STATUSBAR
1598 }
1599
1600 // Event handlers
1601
1602 void RichTextFrame::OnClose(wxCommandEvent& WXUNUSED(event))
1603 {
1604 Close(true);
1605 }
1606
1607 void RichTextFrame::OnLeftAlign(wxCommandEvent& WXUNUSED(event))
1608 {
1609 wxTextAttr attr;
1610 attr.SetAlignment(wxTEXT_ALIGNMENT_LEFT);
1611
1612 long start, end;
1613 m_textCtrl->GetSelection(& start, & end);
1614 m_textCtrl->SetStyle(start, end, attr);
1615
1616 m_currentPosition = -1;
1617 }
1618
1619 void RichTextFrame::OnRightAlign(wxCommandEvent& WXUNUSED(event))
1620 {
1621 wxTextAttr attr;
1622 attr.SetAlignment(wxTEXT_ALIGNMENT_RIGHT);
1623
1624 long start, end;
1625 m_textCtrl->GetSelection(& start, & end);
1626 m_textCtrl->SetStyle(start, end, attr);
1627
1628 m_currentPosition = -1;
1629 }
1630
1631 void RichTextFrame::OnJustify(wxCommandEvent& WXUNUSED(event))
1632 {
1633 wxTextAttr attr;
1634 attr.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED);
1635
1636 long start, end;
1637 m_textCtrl->GetSelection(& start, & end);
1638 m_textCtrl->SetStyle(start, end, attr);
1639
1640 m_currentPosition = -1;
1641 }
1642
1643 void RichTextFrame::OnCentre(wxCommandEvent& WXUNUSED(event))
1644 {
1645 wxTextAttr attr;
1646 attr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
1647
1648 long start, end;
1649 m_textCtrl->GetSelection(& start, & end);
1650 m_textCtrl->SetStyle(start, end, attr);
1651
1652 m_currentPosition = -1;
1653 }
1654
1655 void RichTextFrame::OnChangeFont(wxCommandEvent& WXUNUSED(event))
1656 {
1657 wxFontData data;
1658
1659 wxFontDialog dialog(this, data);
1660
1661 if (dialog.ShowModal() == wxID_OK)
1662 {
1663 wxFontData retData = dialog.GetFontData();
1664 wxFont font = retData.GetChosenFont();
1665
1666 wxTextAttr attr;
1667 attr.SetFont(font);
1668
1669 long start, end;
1670 m_textCtrl->GetSelection(& start, & end);
1671 m_textCtrl->SetStyle(start, end, attr);
1672
1673 m_currentPosition = -1;
1674 }
1675 }
1676
1677 void RichTextFrame::OnChangeTextColour(wxCommandEvent& WXUNUSED(event))
1678 {
1679 wxColourData data;
1680 data.SetColour(* wxBLACK);
1681 data.SetChooseFull(true);
1682 for (int i = 0; i < 16; i++)
1683 {
1684 wxColour colour((unsigned char)(i*16), (unsigned char)(i*16), (unsigned char)(i*16));
1685 data.SetCustomColour(i, colour);
1686 }
1687
1688 wxColourDialog dialog(this, &data);
1689 dialog.SetTitle(_T("Choose the text colour"));
1690 if (dialog.ShowModal() == wxID_OK)
1691 {
1692 wxColourData retData = dialog.GetColourData();
1693 wxColour col = retData.GetColour();
1694
1695 wxTextAttr attr;
1696 attr.SetTextColour(col);
1697
1698 long start, end;
1699 m_textCtrl->GetSelection(& start, & end);
1700 m_textCtrl->SetStyle(start, end, attr);
1701
1702 m_currentPosition = -1;
1703 }
1704 }
1705
1706 void RichTextFrame::OnChangeBackgroundColour(wxCommandEvent& WXUNUSED(event))
1707 {
1708 wxColourData data;
1709 data.SetColour(* wxWHITE);
1710 data.SetChooseFull(true);
1711 for (int i = 0; i < 16; i++)
1712 {
1713 wxColour colour((unsigned char)(i*16), (unsigned char)(i*16), (unsigned char)(i*16));
1714 data.SetCustomColour(i, colour);
1715 }
1716
1717 wxColourDialog dialog(this, &data);
1718 dialog.SetTitle(_T("Choose the text background colour"));
1719 if (dialog.ShowModal() == wxID_OK)
1720 {
1721 wxColourData retData = dialog.GetColourData();
1722 wxColour col = retData.GetColour();
1723
1724 wxTextAttr attr;
1725 attr.SetBackgroundColour(col);
1726
1727 long start, end;
1728 m_textCtrl->GetSelection(& start, & end);
1729 m_textCtrl->SetStyle(start, end, attr);
1730
1731 m_currentPosition = -1;
1732 }
1733 }
1734
1735 void RichTextFrame::OnLeftIndent(wxCommandEvent& WXUNUSED(event))
1736 {
1737 wxString indentStr = wxGetTextFromUser
1738 (
1739 _("Please enter the left indent in tenths of a millimetre."),
1740 _("Left Indent"),
1741 wxEmptyString,
1742 this
1743 );
1744 if (!indentStr.IsEmpty())
1745 {
1746 int indent = wxAtoi(indentStr);
1747
1748 wxTextAttr attr;
1749 attr.SetLeftIndent(indent);
1750
1751 long start, end;
1752 m_textCtrl->GetSelection(& start, & end);
1753 m_textCtrl->SetStyle(start, end, attr);
1754
1755 m_currentPosition = -1;
1756 }
1757 }
1758
1759 void RichTextFrame::OnRightIndent(wxCommandEvent& WXUNUSED(event))
1760 {
1761 wxString indentStr = wxGetTextFromUser
1762 (
1763 _("Please enter the right indent in tenths of a millimetre."),
1764 _("Right Indent"),
1765 wxEmptyString,
1766 this
1767 );
1768 if (!indentStr.IsEmpty())
1769 {
1770 int indent = wxAtoi(indentStr);
1771
1772 wxTextAttr attr;
1773 attr.SetRightIndent(indent);
1774
1775 long start, end;
1776 m_textCtrl->GetSelection(& start, & end);
1777 m_textCtrl->SetStyle(start, end, attr);
1778
1779 m_currentPosition = -1;
1780 }
1781 }
1782
1783 void RichTextFrame::OnTabStops(wxCommandEvent& WXUNUSED(event))
1784 {
1785 wxString tabsStr = wxGetTextFromUser
1786 (
1787 _("Please enter the tab stop positions in tenths of a millimetre, separated by spaces.\nLeave empty to reset tab stops."),
1788 _("Tab Stops"),
1789 wxEmptyString,
1790 this
1791 );
1792
1793 wxArrayInt tabs;
1794
1795 wxStringTokenizer tokens(tabsStr, _T(" "));
1796 while (tokens.HasMoreTokens())
1797 {
1798 wxString token = tokens.GetNextToken();
1799 tabs.Add(wxAtoi(token));
1800 }
1801
1802 wxTextAttr attr;
1803 attr.SetTabs(tabs);
1804
1805 long start, end;
1806 m_textCtrl->GetSelection(& start, & end);
1807 m_textCtrl->SetStyle(start, end, attr);
1808
1809 m_currentPosition = -1;
1810 }
1811
1812 void RichTextFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
1813 {
1814 long insertionPoint = m_textCtrl->GetInsertionPoint();
1815 if (insertionPoint != m_currentPosition)
1816 {
1817 #if wxUSE_STATUSBAR
1818 wxTextAttr attr;
1819 if (m_textCtrl->GetStyle(insertionPoint, attr))
1820 {
1821 wxString msg;
1822 wxString facename(wxT("unknown"));
1823 if (attr.GetFont().Ok())
1824 {
1825 facename = attr.GetFont().GetFaceName();
1826 }
1827 wxString alignment(wxT("unknown alignment"));
1828 if (attr.GetAlignment() == wxTEXT_ALIGNMENT_CENTRE)
1829 alignment = wxT("centred");
1830 else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_RIGHT)
1831 alignment = wxT("right-aligned");
1832 else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_LEFT)
1833 alignment = wxT("left-aligned");
1834 else if (attr.GetAlignment() == wxTEXT_ALIGNMENT_JUSTIFIED)
1835 alignment = wxT("justified");
1836 msg.Printf( "Facename: %s, wxColour(%d, %d, %d), %s", facename,
1837 attr.GetTextColour().Red(), attr.GetTextColour().Green(), attr.GetTextColour().Blue(),
1838 alignment );
1839
1840 if (attr.HasFont())
1841 {
1842 if (attr.GetFont().GetWeight() == wxBOLD)
1843 msg += wxT(" BOLD");
1844 else if (attr.GetFont().GetWeight() == wxNORMAL)
1845 msg += wxT(" NORMAL");
1846
1847 if (attr.GetFont().GetStyle() == wxITALIC)
1848 msg += wxT(" ITALIC");
1849
1850 if (attr.GetFont().GetUnderlined())
1851 msg += wxT(" UNDERLINED");
1852 }
1853
1854 SetStatusText(msg);
1855 }
1856 #endif // wxUSE_STATUSBAR
1857 m_currentPosition = insertionPoint;
1858 }
1859 }