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