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