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