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