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