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