]> git.saurik.com Git - wxWidgets.git/blame - samples/text/text.cpp
wxStaticBitmapBase::DoGetBestSize
[wxWidgets.git] / samples / text / text.cpp
CommitLineData
02e8b87f 1/////////////////////////////////////////////////////////////////////////////
4aff28fc 2// Name: text.cpp
02e8b87f
RR
3// Purpose: TextCtrl wxWindows sample
4// Author: Robert Roebling
5// Modified by:
6// RCS-ID: $Id$
4aff28fc 7// Copyright: (c) Robert Roebling, Julian Smart, Vadim Zeitlin
02e8b87f
RR
8// Licence: wxWindows license
9/////////////////////////////////////////////////////////////////////////////
10
02e8b87f
RR
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
0ddea946 27#if wxUSE_FILE
ae3dd4a5 28 #include "wx/file.h"
0ddea946
VZ
29#endif
30
02e8b87f
RR
31#if wxUSE_TOOLTIPS
32 #include "wx/tooltip.h"
33#endif
34
b6a0d8a2
VZ
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
e00a5d3c
JS
43#include "wx/colordlg.h"
44#include "wx/fontdlg.h"
45
02e8b87f
RR
46//----------------------------------------------------------------------
47// class definitions
48//----------------------------------------------------------------------
49
50class MyApp: public wxApp
51{
52public:
53 bool OnInit();
54};
55
56// a text ctrl which allows to call different wxTextCtrl functions
57// interactively by pressing function keys in it
58class MyTextCtrl : public wxTextCtrl
59{
60public:
61 MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value,
62 const wxPoint &pos, const wxSize &size, int style = 0)
56c4a88b
VZ
63 : wxTextCtrl(parent, id, value, pos, size, style)
64 {
65 m_hasCapture = FALSE;
66 }
02e8b87f
RR
67
68 void OnKeyDown(wxKeyEvent& event);
69 void OnKeyUp(wxKeyEvent& event);
70 void OnChar(wxKeyEvent& event);
d7eee191 71
9ca03a57 72 void OnText(wxCommandEvent& event);
c57e3339 73 void OnTextURL(wxTextUrlEvent& event);
d7eee191
VZ
74 void OnTextMaxLen(wxCommandEvent& event);
75
4aff28fc 76 void OnMouseEvent(wxMouseEvent& event);
02e8b87f 77
94ccc254
VZ
78 void OnSetFocus(wxFocusEvent& event);
79 void OnKillFocus(wxFocusEvent& event);
80
56c4a88b
VZ
81 static bool ms_logKey;
82 static bool ms_logChar;
83 static bool ms_logMouse;
1ce58dc6 84 static bool ms_logText;
94ccc254 85 static bool ms_logFocus;
f152cf5a 86
02e8b87f
RR
87private:
88 static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); }
94ccc254 89 void LogKeyEvent(const wxChar *name, wxKeyEvent& event) const;
02e8b87f 90
56c4a88b
VZ
91 bool m_hasCapture;
92
02e8b87f
RR
93 DECLARE_EVENT_TABLE()
94};
95
96class MyPanel: public wxPanel
97{
98public:
99 MyPanel(wxFrame *frame, int x, int y, int w, int h);
73028c4b 100 virtual ~MyPanel() { delete wxLog::SetActiveTarget(m_logOld); }
02e8b87f 101
b6a0d8a2
VZ
102#if wxUSE_CLIPBOARD
103 void DoPasteFromClipboard();
104 void DoCopyToClipboard();
105#endif // wxUSE_CLIPBOARD
106
cba1148d 107 void DoRemoveText();
cdd732de 108 void DoReplaceText();
29676ca0 109 void DoSelectText();
b6a0d8a2
VZ
110 void DoMoveToEndOfText();
111 void DoMoveToEndOfEntry();
02e8b87f 112
89124b1a
RR
113 void OnSize( wxSizeEvent &event );
114
02e8b87f
RR
115 MyTextCtrl *m_text;
116 MyTextCtrl *m_password;
117 MyTextCtrl *m_enter;
118 MyTextCtrl *m_tab;
02e8b87f 119 MyTextCtrl *m_readonly;
d7eee191 120 MyTextCtrl *m_limited;
bb69661b 121
02e8b87f
RR
122 MyTextCtrl *m_multitext;
123 MyTextCtrl *m_horizontal;
02e8b87f 124
0ddea946
VZ
125 MyTextCtrl *m_textrich;
126
02e8b87f 127 wxTextCtrl *m_log;
89124b1a 128
d847e69a
VZ
129 wxLog *m_logOld;
130
89124b1a 131private:
cba1148d
VZ
132 // get the currently focused text control or return the default one is no
133 // text ctrl has focus
134 wxTextCtrl *GetFocusedText(wxTextCtrl *textDef);
135
89124b1a 136 DECLARE_EVENT_TABLE()
02e8b87f
RR
137};
138
139class MyFrame: public wxFrame
140{
141public:
ab1ca7b3 142 MyFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
02e8b87f
RR
143
144 void OnQuit(wxCommandEvent& event);
145 void OnAbout(wxCommandEvent& event);
146#if wxUSE_TOOLTIPS
147 void OnSetTooltipDelay(wxCommandEvent& event);
148 void OnToggleTooltips(wxCommandEvent& event);
149#endif // wxUSE_TOOLTIPS
b6a0d8a2
VZ
150
151#if wxUSE_CLIPBOARD
656562d8 152 void OnPasteFromClipboard( wxCommandEvent& WXUNUSED(event) )
b6a0d8a2 153 { m_panel->DoPasteFromClipboard(); }
656562d8 154 void OnCopyToClipboard( wxCommandEvent& WXUNUSED(event) )
b6a0d8a2
VZ
155 { m_panel->DoCopyToClipboard(); }
156#endif // wxUSE_CLIPBOARD
157
656562d8 158 void OnAddTextFreeze( wxCommandEvent& WXUNUSED(event) )
eff00523 159 { DoAddText(true); }
656562d8 160 void OnAddText( wxCommandEvent& WXUNUSED(event) )
eff00523 161 { DoAddText(false); }
656562d8 162 void OnRemoveText( wxCommandEvent& WXUNUSED(event) )
cba1148d 163 { m_panel->DoRemoveText(); }
656562d8 164 void OnReplaceText( wxCommandEvent& WXUNUSED(event) )
cdd732de 165 { m_panel->DoReplaceText(); }
656562d8 166 void OnSelectText( wxCommandEvent& WXUNUSED(event) )
29676ca0 167 { m_panel->DoSelectText(); }
eff00523 168
656562d8 169 void OnMoveToEndOfText( wxCommandEvent& WXUNUSED(event) )
b6a0d8a2 170 { m_panel->DoMoveToEndOfText(); }
656562d8 171 void OnMoveToEndOfEntry( wxCommandEvent& WXUNUSED(event) )
b6a0d8a2
VZ
172 { m_panel->DoMoveToEndOfEntry(); }
173
656562d8 174 void OnScrollLineDown(wxCommandEvent& WXUNUSED(event))
22670da4
VZ
175 {
176 if ( !m_panel->m_textrich->LineDown() )
177 wxLogMessage(_T("Already at the bottom"));
178 }
179
656562d8 180 void OnScrollLineUp(wxCommandEvent& WXUNUSED(event))
22670da4
VZ
181 {
182 if ( !m_panel->m_textrich->LineUp() )
183 wxLogMessage(_T("Already at the top"));
184 }
185
656562d8 186 void OnScrollPageDown(wxCommandEvent& WXUNUSED(event))
22670da4
VZ
187 {
188 if ( !m_panel->m_textrich->PageDown() )
189 wxLogMessage(_T("Already at the bottom"));
190 }
191
656562d8 192 void OnScrollPageUp(wxCommandEvent& WXUNUSED(event))
22670da4
VZ
193 {
194 if ( !m_panel->m_textrich->PageUp() )
195 wxLogMessage(_T("Already at the top"));
196 }
197
e66ad5c6 198 void OnLogClear(wxCommandEvent& event);
0ddea946 199 void OnFileSave(wxCommandEvent& event);
b6a0d8a2 200 void OnFileLoad(wxCommandEvent& event);
e00a5d3c 201 void OnRichTextTest(wxCommandEvent& event);
b6a0d8a2 202
f6bcfd97
BP
203 void OnSetEditable(wxCommandEvent& event);
204 void OnSetEnabled(wxCommandEvent& event);
205
56c4a88b
VZ
206 void OnLogKey(wxCommandEvent& event)
207 {
208 MyTextCtrl::ms_logKey = event.IsChecked();
209 }
210
211 void OnLogChar(wxCommandEvent& event)
212 {
213 MyTextCtrl::ms_logChar = event.IsChecked();
214 }
215
216 void OnLogMouse(wxCommandEvent& event)
217 {
218 MyTextCtrl::ms_logMouse = event.IsChecked();
219 }
220
1ce58dc6
VZ
221 void OnLogText(wxCommandEvent& event)
222 {
223 MyTextCtrl::ms_logText = event.IsChecked();
224 }
225
94ccc254
VZ
226 void OnLogFocus(wxCommandEvent& event)
227 {
228 MyTextCtrl::ms_logFocus = event.IsChecked();
229 }
230
656562d8 231 void OnSetText(wxCommandEvent& WXUNUSED(event))
60f52320
VZ
232 {
233 m_panel->m_text->SetValue(_T("Hello, world (what else did you expect)?"));
234 }
235
02e8b87f
RR
236 void OnIdle( wxIdleEvent& event );
237
238private:
eff00523
VZ
239 void DoAddText(bool freeze)
240 {
cdd732de
VZ
241 wxTextCtrl * const text = m_panel->m_textrich;
242 text->Clear();
243
eff00523
VZ
244 if ( freeze )
245 text->Freeze();
246
247 for ( int i = 0; i < 100; i++ )
0146a47e 248 {
4693b20c 249 text->AppendText(wxString::Format(wxT("Line %i\n"), i));
0146a47e 250 }
eff00523 251
eff00523
VZ
252 if ( freeze )
253 text->Thaw();
cdd732de
VZ
254
255 text->SetInsertionPoint(0);
eff00523
VZ
256 }
257
b6a0d8a2
VZ
258 MyPanel *m_panel;
259
02e8b87f
RR
260 DECLARE_EVENT_TABLE()
261};
262
e00a5d3c
JS
263/*
264 * RichTextFrame is used to demonstrate rich text behaviour
265 */
266
267class RichTextFrame: public wxFrame
268{
269public:
270 RichTextFrame(wxWindow* parent, const wxString& title);
271
272// Event handlers
273
274 void OnClose(wxCommandEvent& event);
80185c6c 275 void OnIdle(wxIdleEvent& event);
e00a5d3c
JS
276 void OnLeftAlign(wxCommandEvent& event);
277 void OnRightAlign(wxCommandEvent& event);
278 void OnJustify(wxCommandEvent& event);
279 void OnCentre(wxCommandEvent& event);
280 void OnChangeFont(wxCommandEvent& event);
281 void OnChangeTextColour(wxCommandEvent& event);
282 void OnChangeBackgroundColour(wxCommandEvent& event);
283 void OnLeftIndent(wxCommandEvent& event);
284 void OnRightIndent(wxCommandEvent& event);
285
286private:
287 wxTextCtrl *m_textCtrl;
80185c6c 288 long m_currentPosition;
e00a5d3c
JS
289
290 DECLARE_EVENT_TABLE()
291};
292
02e8b87f
RR
293//----------------------------------------------------------------------
294// main()
295//----------------------------------------------------------------------
296
297IMPLEMENT_APP(MyApp)
298
299//----------------------------------------------------------------------
300// MyApp
301//----------------------------------------------------------------------
302
303enum
304{
b6a0d8a2
VZ
305 TEXT_QUIT = 100,
306 TEXT_ABOUT,
307 TEXT_LOAD,
ae3dd4a5 308 TEXT_SAVE,
e66ad5c6 309 TEXT_CLEAR,
e00a5d3c 310 TEXT_RICH_TEXT_TEST,
b6a0d8a2
VZ
311
312 // clipboard menu
313 TEXT_CLIPBOARD_COPY = 200,
314 TEXT_CLIPBOARD_PASTE,
02e8b87f
RR
315
316 // tooltip menu
b6a0d8a2
VZ
317 TEXT_TOOLTIPS_SETDELAY = 300,
318 TEXT_TOOLTIPS_ENABLE,
319
f6bcfd97 320 // text menu
eff00523
VZ
321 TEXT_ADD_SOME = 400,
322 TEXT_ADD_FREEZE,
323 TEXT_MOVE_ENDTEXT,
f6bcfd97
BP
324 TEXT_MOVE_ENDENTRY,
325 TEXT_SET_EDITABLE,
22670da4
VZ
326 TEXT_SET_ENABLED,
327 TEXT_LINE_DOWN,
328 TEXT_LINE_UP,
329 TEXT_PAGE_DOWN,
330 TEXT_PAGE_UP,
cba1148d 331 TEXT_REMOVE,
cdd732de 332 TEXT_REPLACE,
29676ca0 333 TEXT_SELECT,
60f52320 334 TEXT_SET,
56c4a88b
VZ
335
336 // log menu
337 TEXT_LOG_KEY,
338 TEXT_LOG_CHAR,
339 TEXT_LOG_MOUSE,
1ce58dc6 340 TEXT_LOG_TEXT,
94ccc254 341 TEXT_LOG_FOCUS,
56c4a88b
VZ
342
343 TEXT_END
02e8b87f
RR
344};
345
346bool MyApp::OnInit()
347{
348 // Create the main frame window
349 MyFrame *frame = new MyFrame((wxFrame *) NULL,
ab1ca7b3 350 _T("Text wxWindows sample"), 50, 50, 700, 420);
89124b1a 351 frame->SetSizeHints( 500, 400 );
02e8b87f 352
02e8b87f 353 wxMenu *file_menu = new wxMenu;
ab1ca7b3
MB
354 file_menu->Append(TEXT_SAVE, _T("&Save file\tCtrl-S"),
355 _T("Save the text control contents to file"));
356 file_menu->Append(TEXT_LOAD, _T("&Load file\tCtrl-O"),
357 _T("Load the sample file into text control"));
b6a0d8a2 358 file_menu->AppendSeparator();
e00a5d3c
JS
359 file_menu->Append(TEXT_RICH_TEXT_TEST, _T("Show Rich Text Editor"));
360 file_menu->AppendSeparator();
ab1ca7b3 361 file_menu->Append(TEXT_ABOUT, _T("&About\tAlt-A"));
b6a0d8a2 362 file_menu->AppendSeparator();
ab1ca7b3 363 file_menu->Append(TEXT_QUIT, _T("E&xit\tAlt-X"), _T("Quit this sample"));
02e8b87f
RR
364
365 wxMenuBar *menu_bar = new wxMenuBar( wxMB_DOCKABLE );
ab1ca7b3 366 menu_bar->Append(file_menu, _T("&File"));
02e8b87f
RR
367
368#if wxUSE_TOOLTIPS
369 wxMenu *tooltip_menu = new wxMenu;
ab1ca7b3 370 tooltip_menu->Append(TEXT_TOOLTIPS_SETDELAY, _T("Set &delay\tCtrl-D"));
02e8b87f 371 tooltip_menu->AppendSeparator();
ab1ca7b3
MB
372 tooltip_menu->Append(TEXT_TOOLTIPS_ENABLE, _T("&Toggle tooltips\tCtrl-T"),
373 _T("enable/disable tooltips"), TRUE);
b6a0d8a2 374 tooltip_menu->Check(TEXT_TOOLTIPS_ENABLE, TRUE);
ab1ca7b3 375 menu_bar->Append(tooltip_menu, _T("&Tooltips"));
02e8b87f
RR
376#endif // wxUSE_TOOLTIPS
377
b6a0d8a2
VZ
378#if wxUSE_CLIPBOARD
379 wxMenu *menuClipboard = new wxMenu;
ab1ca7b3
MB
380 menuClipboard->Append(TEXT_CLIPBOARD_COPY, _T("&Copy\tCtrl-C"),
381 _T("Copy the first line to the clipboard"));
382 menuClipboard->Append(TEXT_CLIPBOARD_PASTE, _T("&Paste\tCtrl-V"),
383 _T("Paste from clipboard to the text control"));
384 menu_bar->Append(menuClipboard, _T("&Clipboard"));
b6a0d8a2
VZ
385#endif // wxUSE_CLIPBOARD
386
f6bcfd97 387 wxMenu *menuText = new wxMenu;
ab1ca7b3
MB
388 menuText->Append(TEXT_ADD_SOME, _T("&Append some text\tCtrl-A"));
389 menuText->Append(TEXT_ADD_FREEZE, _T("&Append text with freeze/thaw\tShift-Ctrl-A"));
cdd732de
VZ
390 menuText->Append(TEXT_REMOVE, _T("&Remove first 10 characters\tCtrl-Y"));
391 menuText->Append(TEXT_REPLACE, _T("&Replace characters 4 to 8 with ABC\tCtrl-R"));
29676ca0 392 menuText->Append(TEXT_SELECT, _T("&Select characters 4 to 8\tCtrl-I"));
ab1ca7b3 393 menuText->Append(TEXT_SET, _T("&Set the first text zone value\tCtrl-E"));
cba1148d 394 menuText->AppendSeparator();
ab1ca7b3
MB
395 menuText->Append(TEXT_MOVE_ENDTEXT, _T("Move cursor to the end of &text"));
396 menuText->Append(TEXT_MOVE_ENDENTRY, _T("Move cursor to the end of &entry"));
397 menuText->Append(TEXT_SET_EDITABLE, _T("Toggle &editable state"), _T(""), TRUE);
398 menuText->Append(TEXT_SET_ENABLED, _T("Toggle e&nabled state"), _T(""), TRUE);
f6bcfd97
BP
399 menuText->Check(TEXT_SET_EDITABLE, TRUE);
400 menuText->Check(TEXT_SET_ENABLED, TRUE);
22670da4 401 menuText->AppendSeparator();
ab1ca7b3
MB
402 menuText->Append(TEXT_LINE_DOWN, _T("Scroll text one line down"));
403 menuText->Append(TEXT_LINE_UP, _T("Scroll text one line up"));
404 menuText->Append(TEXT_PAGE_DOWN, _T("Scroll text one page down"));
405 menuText->Append(TEXT_PAGE_DOWN, _T("Scroll text one page up"));
406 menu_bar->Append(menuText, _T("Te&xt"));
56c4a88b
VZ
407
408 wxMenu *menuLog = new wxMenu;
ab1ca7b3
MB
409 menuLog->Append(TEXT_LOG_KEY, _T("Log &key events"), _T(""), TRUE);
410 menuLog->Append(TEXT_LOG_CHAR, _T("Log &char events"), _T(""), TRUE);
411 menuLog->Append(TEXT_LOG_MOUSE, _T("Log &mouse events"), _T(""), TRUE);
412 menuLog->Append(TEXT_LOG_TEXT, _T("Log &text events"), _T(""), TRUE);
413 menuLog->Append(TEXT_LOG_FOCUS, _T("Log &focus events"), _T(""), TRUE);
56c4a88b 414 menuLog->AppendSeparator();
ab1ca7b3
MB
415 menuLog->Append(TEXT_CLEAR, _T("&Clear the log\tCtrl-C"),
416 _T("Clear the log window contents"));
94ccc254
VZ
417
418 // select only the interesting events by default
56c4a88b
VZ
419 menuLog->Check(TEXT_LOG_KEY, TRUE);
420 menuLog->Check(TEXT_LOG_CHAR, TRUE);
1ce58dc6 421 menuLog->Check(TEXT_LOG_TEXT, TRUE);
94ccc254
VZ
422
423 MyTextCtrl::ms_logKey =
424 MyTextCtrl::ms_logChar =
425 MyTextCtrl::ms_logText = TRUE;
ab1ca7b3 426 menu_bar->Append(menuLog, _T("&Log"));
b6a0d8a2 427
02e8b87f
RR
428 frame->SetMenuBar(menu_bar);
429
430 frame->Show(TRUE);
431
432 SetTopWindow(frame);
433
434 // report success
435 return TRUE;
436}
437
438//----------------------------------------------------------------------
439// MyTextCtrl
440//----------------------------------------------------------------------
441
442BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl)
443 EVT_KEY_DOWN(MyTextCtrl::OnKeyDown)
444 EVT_KEY_UP(MyTextCtrl::OnKeyUp)
445 EVT_CHAR(MyTextCtrl::OnChar)
d7eee191 446
9ca03a57 447 EVT_TEXT(-1, MyTextCtrl::OnText)
c57e3339 448 EVT_TEXT_URL(-1, MyTextCtrl::OnTextURL)
d7eee191
VZ
449 EVT_TEXT_MAXLEN(-1, MyTextCtrl::OnTextMaxLen)
450
4aff28fc 451 EVT_MOUSE_EVENTS(MyTextCtrl::OnMouseEvent)
94ccc254
VZ
452
453 EVT_SET_FOCUS(MyTextCtrl::OnSetFocus)
454 EVT_KILL_FOCUS(MyTextCtrl::OnKillFocus)
02e8b87f
RR
455END_EVENT_TABLE()
456
94ccc254
VZ
457bool MyTextCtrl::ms_logKey = FALSE;
458bool MyTextCtrl::ms_logChar = FALSE;
fb93b610 459bool MyTextCtrl::ms_logMouse = FALSE;
94ccc254
VZ
460bool MyTextCtrl::ms_logText = FALSE;
461bool MyTextCtrl::ms_logFocus = FALSE;
56c4a88b 462
94ccc254 463void MyTextCtrl::LogKeyEvent(const wxChar *name, wxKeyEvent& event) const
02e8b87f
RR
464{
465 wxString key;
b1d4dd7a 466 long keycode = event.GetKeyCode();
02e8b87f
RR
467 {
468 switch ( keycode )
469 {
ab1ca7b3
MB
470 case WXK_BACK: key = _T("BACK"); break;
471 case WXK_TAB: key = _T("TAB"); break;
472 case WXK_RETURN: key = _T("RETURN"); break;
473 case WXK_ESCAPE: key = _T("ESCAPE"); break;
474 case WXK_SPACE: key = _T("SPACE"); break;
475 case WXK_DELETE: key = _T("DELETE"); break;
476 case WXK_START: key = _T("START"); break;
477 case WXK_LBUTTON: key = _T("LBUTTON"); break;
478 case WXK_RBUTTON: key = _T("RBUTTON"); break;
479 case WXK_CANCEL: key = _T("CANCEL"); break;
480 case WXK_MBUTTON: key = _T("MBUTTON"); break;
481 case WXK_CLEAR: key = _T("CLEAR"); break;
482 case WXK_SHIFT: key = _T("SHIFT"); break;
483 case WXK_ALT: key = _T("ALT"); break;
484 case WXK_CONTROL: key = _T("CONTROL"); break;
485 case WXK_MENU: key = _T("MENU"); break;
486 case WXK_PAUSE: key = _T("PAUSE"); break;
487 case WXK_CAPITAL: key = _T("CAPITAL"); break;
488 case WXK_PRIOR: key = _T("PRIOR"); break;
489 case WXK_NEXT: key = _T("NEXT"); break;
490 case WXK_END: key = _T("END"); break;
491 case WXK_HOME: key = _T("HOME"); break;
492 case WXK_LEFT: key = _T("LEFT"); break;
493 case WXK_UP: key = _T("UP"); break;
494 case WXK_RIGHT: key = _T("RIGHT"); break;
495 case WXK_DOWN: key = _T("DOWN"); break;
496 case WXK_SELECT: key = _T("SELECT"); break;
497 case WXK_PRINT: key = _T("PRINT"); break;
498 case WXK_EXECUTE: key = _T("EXECUTE"); break;
499 case WXK_SNAPSHOT: key = _T("SNAPSHOT"); break;
500 case WXK_INSERT: key = _T("INSERT"); break;
501 case WXK_HELP: key = _T("HELP"); break;
502 case WXK_NUMPAD0: key = _T("NUMPAD0"); break;
503 case WXK_NUMPAD1: key = _T("NUMPAD1"); break;
504 case WXK_NUMPAD2: key = _T("NUMPAD2"); break;
505 case WXK_NUMPAD3: key = _T("NUMPAD3"); break;
506 case WXK_NUMPAD4: key = _T("NUMPAD4"); break;
507 case WXK_NUMPAD5: key = _T("NUMPAD5"); break;
508 case WXK_NUMPAD6: key = _T("NUMPAD6"); break;
509 case WXK_NUMPAD7: key = _T("NUMPAD7"); break;
510 case WXK_NUMPAD8: key = _T("NUMPAD8"); break;
511 case WXK_NUMPAD9: key = _T("NUMPAD9"); break;
512 case WXK_MULTIPLY: key = _T("MULTIPLY"); break;
513 case WXK_ADD: key = _T("ADD"); break;
514 case WXK_SEPARATOR: key = _T("SEPARATOR"); break;
515 case WXK_SUBTRACT: key = _T("SUBTRACT"); break;
516 case WXK_DECIMAL: key = _T("DECIMAL"); break;
517 case WXK_DIVIDE: key = _T("DIVIDE"); break;
518 case WXK_F1: key = _T("F1"); break;
519 case WXK_F2: key = _T("F2"); break;
520 case WXK_F3: key = _T("F3"); break;
521 case WXK_F4: key = _T("F4"); break;
522 case WXK_F5: key = _T("F5"); break;
523 case WXK_F6: key = _T("F6"); break;
524 case WXK_F7: key = _T("F7"); break;
525 case WXK_F8: key = _T("F8"); break;
526 case WXK_F9: key = _T("F9"); break;
527 case WXK_F10: key = _T("F10"); break;
528 case WXK_F11: key = _T("F11"); break;
529 case WXK_F12: key = _T("F12"); break;
530 case WXK_F13: key = _T("F13"); break;
531 case WXK_F14: key = _T("F14"); break;
532 case WXK_F15: key = _T("F15"); break;
533 case WXK_F16: key = _T("F16"); break;
534 case WXK_F17: key = _T("F17"); break;
535 case WXK_F18: key = _T("F18"); break;
536 case WXK_F19: key = _T("F19"); break;
537 case WXK_F20: key = _T("F20"); break;
538 case WXK_F21: key = _T("F21"); break;
539 case WXK_F22: key = _T("F22"); break;
540 case WXK_F23: key = _T("F23"); break;
541 case WXK_F24: key = _T("F24"); break;
542 case WXK_NUMLOCK: key = _T("NUMLOCK"); break;
543 case WXK_SCROLL: key = _T("SCROLL"); break;
544 case WXK_PAGEUP: key = _T("PAGEUP"); break;
545 case WXK_PAGEDOWN: key = _T("PAGEDOWN"); break;
546 case WXK_NUMPAD_SPACE: key = _T("NUMPAD_SPACE"); break;
547 case WXK_NUMPAD_TAB: key = _T("NUMPAD_TAB"); break;
548 case WXK_NUMPAD_ENTER: key = _T("NUMPAD_ENTER"); break;
549 case WXK_NUMPAD_F1: key = _T("NUMPAD_F1"); break;
550 case WXK_NUMPAD_F2: key = _T("NUMPAD_F2"); break;
551 case WXK_NUMPAD_F3: key = _T("NUMPAD_F3"); break;
552 case WXK_NUMPAD_F4: key = _T("NUMPAD_F4"); break;
553 case WXK_NUMPAD_HOME: key = _T("NUMPAD_HOME"); break;
554 case WXK_NUMPAD_LEFT: key = _T("NUMPAD_LEFT"); break;
555 case WXK_NUMPAD_UP: key = _T("NUMPAD_UP"); break;
556 case WXK_NUMPAD_RIGHT: key = _T("NUMPAD_RIGHT"); break;
557 case WXK_NUMPAD_DOWN: key = _T("NUMPAD_DOWN"); break;
558 case WXK_NUMPAD_PRIOR: key = _T("NUMPAD_PRIOR"); break;
559 case WXK_NUMPAD_PAGEUP: key = _T("NUMPAD_PAGEUP"); break;
560 case WXK_NUMPAD_PAGEDOWN: key = _T("NUMPAD_PAGEDOWN"); break;
561 case WXK_NUMPAD_END: key = _T("NUMPAD_END"); break;
562 case WXK_NUMPAD_BEGIN: key = _T("NUMPAD_BEGIN"); break;
563 case WXK_NUMPAD_INSERT: key = _T("NUMPAD_INSERT"); break;
564 case WXK_NUMPAD_DELETE: key = _T("NUMPAD_DELETE"); break;
565 case WXK_NUMPAD_EQUAL: key = _T("NUMPAD_EQUAL"); break;
566 case WXK_NUMPAD_MULTIPLY: key = _T("NUMPAD_MULTIPLY"); break;
567 case WXK_NUMPAD_ADD: key = _T("NUMPAD_ADD"); break;
568 case WXK_NUMPAD_SEPARATOR: key = _T("NUMPAD_SEPARATOR"); break;
569 case WXK_NUMPAD_SUBTRACT: key = _T("NUMPAD_SUBTRACT"); break;
570 case WXK_NUMPAD_DECIMAL: key = _T("NUMPAD_DECIMAL"); break;
02e8b87f 571
4afd7529 572 default:
89124b1a
RR
573 {
574 if ( wxIsprint((int)keycode) )
1ce58dc6
VZ
575 key.Printf(_T("'%c'"), (char)keycode);
576 else if ( keycode > 0 && keycode < 27 )
577 key.Printf(_("Ctrl-%c"), _T('A') + keycode - 1);
89124b1a 578 else
1ce58dc6 579 key.Printf(_T("unknown (%ld)"), keycode);
4afd7529 580 }
02e8b87f
RR
581 }
582 }
583
584 wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"),
585 name,
586 key.c_str(),
587 GetChar( event.ControlDown(), _T('C') ),
588 GetChar( event.AltDown(), _T('A') ),
589 GetChar( event.ShiftDown(), _T('S') ),
590 GetChar( event.MetaDown(), _T('M') ) );
02e8b87f
RR
591}
592
c57e3339
VZ
593static wxString GetMouseEventDesc(const wxMouseEvent& ev)
594{
595 // click event
596 wxString button;
597 bool dbl, up;
598 if ( ev.LeftDown() || ev.LeftUp() || ev.LeftDClick() )
599 {
600 button = _T("Left");
601 dbl = ev.LeftDClick();
602 up = ev.LeftUp();
603 }
604 else if ( ev.MiddleDown() || ev.MiddleUp() || ev.MiddleDClick() )
605 {
606 button = _T("Middle");
607 dbl = ev.MiddleDClick();
608 up = ev.MiddleUp();
609 }
610 else if ( ev.RightDown() || ev.RightUp() || ev.RightDClick() )
611 {
612 button = _T("Right");
613 dbl = ev.RightDClick();
614 up = ev.RightUp();
615 }
616 else
617 {
618 return _T("Unknown mouse event");
619 }
620
621 return wxString::Format(_T("%s mouse button %s"),
622 button.c_str(),
623 dbl ? _T("double clicked")
624 : up ? _T("released") : _T("clicked"));
625}
626
4aff28fc
VZ
627void MyTextCtrl::OnMouseEvent(wxMouseEvent& ev)
628{
56c4a88b
VZ
629 ev.Skip();
630
1ce58dc6 631 if ( !ms_logMouse )
56c4a88b
VZ
632 return;
633
4aff28fc
VZ
634 if ( !ev.Moving() )
635 {
636 wxString msg;
637 if ( ev.Entering() )
638 {
639 msg = _T("Mouse entered the window");
640 }
641 else if ( ev.Leaving() )
642 {
643 msg = _T("Mouse left the window");
644 }
645 else
646 {
c57e3339 647 msg = GetMouseEventDesc(ev);
4aff28fc
VZ
648 }
649
650 msg << _T(" at (") << ev.GetX() << _T(", ") << ev.GetY() << _T(") ")
651 << _T("Flags: ")
4ed0aceb
VZ
652 << GetChar( ev.LeftIsDown(), _T('1') )
653 << GetChar( ev.MiddleIsDown(), _T('2') )
654 << GetChar( ev.RightIsDown(), _T('3') )
4aff28fc
VZ
655 << GetChar( ev.ControlDown(), _T('C') )
656 << GetChar( ev.AltDown(), _T('A') )
657 << GetChar( ev.ShiftDown(), _T('S') )
658 << GetChar( ev.MetaDown(), _T('M') );
659
660 wxLogMessage(msg);
661 }
662 //else: we're not interested in mouse move events
663}
664
94ccc254
VZ
665void MyTextCtrl::OnSetFocus(wxFocusEvent& event)
666{
667 if ( ms_logFocus )
eba33006 668 wxLogMessage( wxT("%p got focus."), this);
94ccc254
VZ
669
670 event.Skip();
671}
672
673void MyTextCtrl::OnKillFocus(wxFocusEvent& event)
674{
675 if ( ms_logFocus )
eba33006 676 wxLogMessage( wxT("%p lost focus"), this);
94ccc254
VZ
677
678 event.Skip();
679}
680
9ca03a57 681void MyTextCtrl::OnText(wxCommandEvent& event)
02e8b87f 682{
1ce58dc6
VZ
683 if ( !ms_logText )
684 return;
685
9ca03a57 686 MyTextCtrl *win = (MyTextCtrl *)event.GetEventObject();
ae3dd4a5
VZ
687 const wxChar *data = (const wxChar *)(win->GetClientData());
688 if ( data )
689 {
c41ea66a 690 wxLogMessage(_T("Text changed in control '%s'"), data);
ae3dd4a5 691 }
f6bcfd97
BP
692 else
693 {
eba33006 694 // wxLogMessage( event.GetString() );
f6bcfd97
BP
695 wxLogMessage(_T("Text changed in some control"));
696 }
9ca03a57
VZ
697}
698
656562d8 699void MyTextCtrl::OnTextMaxLen(wxCommandEvent& WXUNUSED(event))
d7eee191
VZ
700{
701 wxLogMessage(_T("You can't enter more characters into this control."));
702}
703
c57e3339
VZ
704void MyTextCtrl::OnTextURL(wxTextUrlEvent& event)
705{
706 const wxMouseEvent& ev = event.GetMouseEvent();
707
708 // filter out mouse moves, too many of them
709 if ( ev.Moving() )
710 return;
711
712 long start = event.GetURLStart(),
713 end = event.GetURLEnd();
714
715 wxLogMessage(_T("Mouse event over URL '%s': %s"),
716 GetValue().Mid(start, end - start).c_str(),
717 GetMouseEventDesc(ev).c_str());
718}
719
9ca03a57
VZ
720void MyTextCtrl::OnChar(wxKeyEvent& event)
721{
1ce58dc6 722 if ( ms_logChar )
94ccc254 723 LogKeyEvent( _T("Char"), event);
3017f78d 724
3017f78d 725 event.Skip();
02e8b87f
RR
726}
727
728void MyTextCtrl::OnKeyUp(wxKeyEvent& event)
729{
1ce58dc6 730 if ( ms_logKey )
94ccc254 731 LogKeyEvent( _T("Key up"), event);
02e8b87f
RR
732
733 event.Skip();
734}
735
736void MyTextCtrl::OnKeyDown(wxKeyEvent& event)
737{
b1d4dd7a 738 switch ( event.GetKeyCode() )
02e8b87f
RR
739 {
740 case WXK_F1:
741 // show current position and text length
742 {
743 long line, column, pos = GetInsertionPoint();
744 PositionToXY(pos, &column, &line);
745
2b5f62a0 746 wxLogMessage(_T("Current position: %ld\nCurrent line, column: (%ld, %ld)\nNumber of lines: %ld\nCurrent line length: %ld\nTotal text length: %u (%ld)"),
02e8b87f
RR
747 pos,
748 line, column,
8b1d8f36
VZ
749 (long) GetNumberOfLines(),
750 (long) GetLineLength(line),
f6bcfd97 751 GetValue().length(),
02e8b87f 752 GetLastPosition());
2b5f62a0
VZ
753
754 long from, to;
755 GetSelection(&from, &to);
756
757 wxString sel = GetStringSelection();
758
759 wxLogMessage(_T("Selection: from %ld to %ld."), from, to);
760 wxLogMessage(_T("Selection = '%s' (len = %u)"),
761 sel.c_str(), sel.length());
02e8b87f
RR
762 }
763 break;
764
765 case WXK_F2:
766 // go to the end
767 SetInsertionPointEnd();
768 break;
769
770 case WXK_F3:
771 // go to position 10
772 SetInsertionPoint(10);
773 break;
4afd7529 774
f152cf5a 775 case WXK_F4:
4afd7529
VZ
776 if (!m_hasCapture)
777 {
f152cf5a 778 wxLogDebug( wxT("Now capturing mouse and events.") );
4afd7529
VZ
779 m_hasCapture = TRUE;
780 CaptureMouse();
781 }
782 else
783 {
f152cf5a 784 wxLogDebug( wxT("Stopped capturing mouse and events.") );
e3603065 785 m_hasCapture = FALSE;
4afd7529
VZ
786 ReleaseMouse();
787 }
f152cf5a 788 break;
4afd7529
VZ
789
790 case WXK_F5:
791 // insert a blank line
ab1ca7b3 792 WriteText(_T("\n"));
ae3dd4a5 793 break;
02e8b87f 794
f6bcfd97 795 case WXK_F6:
2b5f62a0
VZ
796 wxLogMessage(_T("IsModified() before SetValue(): %d"),
797 IsModified());
798 SetValue(_T("SetValue() has been called"));
799 wxLogMessage(_T("IsModified() after SetValue(): %d"),
800 IsModified());
f6bcfd97
BP
801 break;
802
803 case WXK_F7:
2b5f62a0 804 wxLogMessage(_T("Position 10 should be now visible."));
f6bcfd97
BP
805 ShowPosition(10);
806 break;
18414479 807
80695f83 808 case WXK_F8:
2b5f62a0 809 wxLogMessage(_T("Control has been cleared"));
80695f83
VZ
810 Clear();
811 break;
812
2b5f62a0 813 case WXK_F9:
ab1ca7b3 814 WriteText(_T("WriteText() has been called"));
2b5f62a0 815 break;
18414479 816
2b5f62a0 817 case WXK_F10:
ab1ca7b3 818 AppendText(_T("AppendText() has been called"));
2b5f62a0 819 break;
0ddea946 820 }
02e8b87f 821
1ce58dc6 822 if ( ms_logKey )
94ccc254 823 LogKeyEvent( wxT("Key down"), event);
f6bcfd97 824
02e8b87f
RR
825 event.Skip();
826}
827
828//----------------------------------------------------------------------
829// MyPanel
830//----------------------------------------------------------------------
831
89124b1a
RR
832BEGIN_EVENT_TABLE(MyPanel, wxPanel)
833 EVT_SIZE(MyPanel::OnSize)
834END_EVENT_TABLE()
835
02e8b87f
RR
836MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
837 : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) )
838{
ab1ca7b3 839 m_log = new wxTextCtrl( this, -1, _T("This is the log window.\n"),
30982415
VZ
840 wxPoint(5,260), wxSize(630,100),
841 wxTE_MULTILINE | wxTE_READONLY /* | wxTE_RICH */);
02e8b87f 842
d847e69a 843 m_logOld = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) );
02e8b87f
RR
844
845 // single line text controls
846
ab1ca7b3 847 m_text = new MyTextCtrl( this, -1, _T("Single line."),
f6bcfd97
BP
848 wxPoint(10,10), wxSize(140,-1),
849 wxTE_PROCESS_ENTER);
4ed0aceb
VZ
850 m_text->SetForegroundColour(*wxBLUE);
851 m_text->SetBackgroundColour(*wxLIGHT_GREY);
ab1ca7b3 852 (*m_text) << _T(" Appended.");
02e8b87f 853 m_text->SetInsertionPoint(0);
ab1ca7b3 854 m_text->WriteText( _T("Prepended. ") );
bb69661b 855
ab1ca7b3 856 m_password = new MyTextCtrl( this, -1, _T(""),
02e8b87f 857 wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD );
bb69661b 858
ab1ca7b3 859 m_readonly = new MyTextCtrl( this, -1, _T("Read only"),
02e8b87f 860 wxPoint(10,90), wxSize(140,-1), wxTE_READONLY );
bb69661b 861
ab1ca7b3 862 m_limited = new MyTextCtrl(this, -1, _T("Max 8 ch"),
d7eee191
VZ
863 wxPoint(10, 130), wxSize(140, -1));
864 m_limited->SetMaxLength(8);
865
02e8b87f
RR
866 // multi line text controls
867
ab1ca7b3 868 m_horizontal = new MyTextCtrl( this, -1, _T("Multiline text control with a horizontal scrollbar."),
bb69661b 869 wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL );
f6bcfd97
BP
870
871 // a little hack to use the command line argument for encoding testing
872 if ( wxTheApp->argc == 2 )
873 {
874 switch ( wxTheApp->argv[1][0] )
875 {
876 case '2':
877 m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL,
ab1ca7b3 878 FALSE, _T(""),
f6bcfd97 879 wxFONTENCODING_ISO8859_2));
ab1ca7b3 880