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