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