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