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