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