]>
Commit | Line | Data |
---|---|---|
02e8b87f RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: controls.cpp | |
3 | // Purpose: TextCtrl wxWindows sample | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Robert Roebling, Julian Smart | |
8 | // Licence: wxWindows license | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "controls.h" | |
13 | #endif | |
14 | ||
15 | // For compilers that support precompilation, includes "wx/wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/wx.h" | |
24 | #endif | |
25 | ||
26 | #if wxUSE_CLIPBOARD | |
27 | #include "wx/dataobj.h" | |
28 | #include "wx/clipbrd.h" | |
29 | #endif | |
30 | ||
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 | ||
02e8b87f RR |
43 | //---------------------------------------------------------------------- |
44 | // class definitions | |
45 | //---------------------------------------------------------------------- | |
46 | ||
47 | class MyApp: public wxApp | |
48 | { | |
49 | public: | |
50 | bool OnInit(); | |
51 | }; | |
52 | ||
53 | // a text ctrl which allows to call different wxTextCtrl functions | |
54 | // interactively by pressing function keys in it | |
55 | class MyTextCtrl : public wxTextCtrl | |
56 | { | |
57 | public: | |
58 | MyTextCtrl(wxWindow *parent, wxWindowID id, const wxString &value, | |
59 | const wxPoint &pos, const wxSize &size, int style = 0) | |
60 | : wxTextCtrl(parent, id, value, pos, size, style) { } | |
61 | ||
62 | void OnKeyDown(wxKeyEvent& event); | |
63 | void OnKeyUp(wxKeyEvent& event); | |
64 | void OnChar(wxKeyEvent& event); | |
65 | ||
66 | private: | |
67 | static inline wxChar GetChar(bool on, wxChar c) { return on ? c : _T('-'); } | |
68 | void LogEvent(const wxChar *name, wxKeyEvent& event) const; | |
69 | ||
70 | DECLARE_EVENT_TABLE() | |
71 | }; | |
72 | ||
73 | class MyPanel: public wxPanel | |
74 | { | |
75 | public: | |
76 | MyPanel(wxFrame *frame, int x, int y, int w, int h); | |
77 | ||
b6a0d8a2 VZ |
78 | #if wxUSE_CLIPBOARD |
79 | void DoPasteFromClipboard(); | |
80 | void DoCopyToClipboard(); | |
81 | #endif // wxUSE_CLIPBOARD | |
82 | ||
83 | void DoMoveToEndOfText(); | |
84 | void DoMoveToEndOfEntry(); | |
02e8b87f | 85 | |
89124b1a RR |
86 | void OnSize( wxSizeEvent &event ); |
87 | ||
02e8b87f RR |
88 | MyTextCtrl *m_text; |
89 | MyTextCtrl *m_password; | |
90 | MyTextCtrl *m_enter; | |
91 | MyTextCtrl *m_tab; | |
02e8b87f | 92 | MyTextCtrl *m_readonly; |
bb69661b | 93 | |
02e8b87f RR |
94 | MyTextCtrl *m_multitext; |
95 | MyTextCtrl *m_horizontal; | |
02e8b87f RR |
96 | |
97 | wxTextCtrl *m_log; | |
89124b1a RR |
98 | |
99 | private: | |
100 | DECLARE_EVENT_TABLE() | |
02e8b87f RR |
101 | }; |
102 | ||
103 | class MyFrame: public wxFrame | |
104 | { | |
105 | public: | |
b6a0d8a2 | 106 | MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h); |
02e8b87f RR |
107 | |
108 | void OnQuit(wxCommandEvent& event); | |
109 | void OnAbout(wxCommandEvent& event); | |
110 | #if wxUSE_TOOLTIPS | |
111 | void OnSetTooltipDelay(wxCommandEvent& event); | |
112 | void OnToggleTooltips(wxCommandEvent& event); | |
113 | #endif // wxUSE_TOOLTIPS | |
b6a0d8a2 VZ |
114 | |
115 | #if wxUSE_CLIPBOARD | |
116 | void OnPasteFromClipboard( wxCommandEvent &event ) | |
117 | { m_panel->DoPasteFromClipboard(); } | |
118 | void OnCopyToClipboard( wxCommandEvent &event ) | |
119 | { m_panel->DoCopyToClipboard(); } | |
120 | #endif // wxUSE_CLIPBOARD | |
121 | ||
122 | void OnMoveToEndOfText( wxCommandEvent &event ) | |
123 | { m_panel->DoMoveToEndOfText(); } | |
124 | void OnMoveToEndOfEntry( wxCommandEvent &event ) | |
125 | { m_panel->DoMoveToEndOfEntry(); } | |
126 | ||
127 | void OnFileLoad(wxCommandEvent& event); | |
128 | ||
02e8b87f RR |
129 | void OnIdle( wxIdleEvent& event ); |
130 | ||
131 | private: | |
b6a0d8a2 VZ |
132 | MyPanel *m_panel; |
133 | ||
02e8b87f RR |
134 | DECLARE_EVENT_TABLE() |
135 | }; | |
136 | ||
137 | //---------------------------------------------------------------------- | |
138 | // main() | |
139 | //---------------------------------------------------------------------- | |
140 | ||
141 | IMPLEMENT_APP(MyApp) | |
142 | ||
143 | //---------------------------------------------------------------------- | |
144 | // MyApp | |
145 | //---------------------------------------------------------------------- | |
146 | ||
147 | enum | |
148 | { | |
b6a0d8a2 VZ |
149 | TEXT_QUIT = 100, |
150 | TEXT_ABOUT, | |
151 | TEXT_LOAD, | |
152 | ||
153 | // clipboard menu | |
154 | TEXT_CLIPBOARD_COPY = 200, | |
155 | TEXT_CLIPBOARD_PASTE, | |
02e8b87f RR |
156 | |
157 | // tooltip menu | |
b6a0d8a2 VZ |
158 | TEXT_TOOLTIPS_SETDELAY = 300, |
159 | TEXT_TOOLTIPS_ENABLE, | |
160 | ||
161 | // move menu | |
162 | TEXT_MOVE_ENDTEXT = 400, | |
163 | TEXT_MOVE_ENDENTRY | |
02e8b87f RR |
164 | }; |
165 | ||
166 | bool MyApp::OnInit() | |
167 | { | |
168 | // Create the main frame window | |
169 | MyFrame *frame = new MyFrame((wxFrame *) NULL, | |
ddc8c2e3 | 170 | "Text wxWindows sample", 50, 50, 640, 420); |
89124b1a | 171 | frame->SetSizeHints( 500, 400 ); |
02e8b87f | 172 | |
02e8b87f | 173 | wxMenu *file_menu = new wxMenu; |
b6a0d8a2 VZ |
174 | file_menu->Append(TEXT_LOAD, "&Load file\tCtrl-O", |
175 | "Load the sample file into text control"); | |
176 | file_menu->AppendSeparator(); | |
177 | file_menu->Append(TEXT_ABOUT, "&About\tAlt-A"); | |
178 | file_menu->AppendSeparator(); | |
179 | file_menu->Append(TEXT_QUIT, "E&xit\tAlt-X", "Quit controls sample"); | |
02e8b87f RR |
180 | |
181 | wxMenuBar *menu_bar = new wxMenuBar( wxMB_DOCKABLE ); | |
182 | menu_bar->Append(file_menu, "&File"); | |
183 | ||
184 | #if wxUSE_TOOLTIPS | |
185 | wxMenu *tooltip_menu = new wxMenu; | |
b6a0d8a2 | 186 | tooltip_menu->Append(TEXT_TOOLTIPS_SETDELAY, "Set &delay\tCtrl-D"); |
02e8b87f | 187 | tooltip_menu->AppendSeparator(); |
b6a0d8a2 | 188 | tooltip_menu->Append(TEXT_TOOLTIPS_ENABLE, "&Toggle tooltips\tCtrl-T", |
02e8b87f | 189 | "enable/disable tooltips", TRUE); |
b6a0d8a2 | 190 | tooltip_menu->Check(TEXT_TOOLTIPS_ENABLE, TRUE); |
02e8b87f RR |
191 | menu_bar->Append(tooltip_menu, "&Tooltips"); |
192 | #endif // wxUSE_TOOLTIPS | |
193 | ||
b6a0d8a2 VZ |
194 | #if wxUSE_CLIPBOARD |
195 | wxMenu *menuClipboard = new wxMenu; | |
196 | menuClipboard->Append(TEXT_CLIPBOARD_COPY, "&Copy\tCtrl-C", | |
197 | "Copy the first line to the clipboard"); | |
198 | menuClipboard->Append(TEXT_CLIPBOARD_PASTE, "&Paste\tCtrl-V", | |
199 | "Paste from clipboard to the text control"); | |
200 | menu_bar->Append(menuClipboard, "&Clipboard"); | |
201 | #endif // wxUSE_CLIPBOARD | |
202 | ||
203 | wxMenu *menuMove = new wxMenu; | |
204 | menuMove->Append(TEXT_MOVE_ENDTEXT, "To the end of &text"); | |
205 | menuMove->Append(TEXT_MOVE_ENDENTRY, "To the end of &entry"); | |
206 | menu_bar->Append(menuMove, "&Move"); | |
207 | ||
02e8b87f RR |
208 | frame->SetMenuBar(menu_bar); |
209 | ||
210 | frame->Show(TRUE); | |
211 | ||
212 | SetTopWindow(frame); | |
213 | ||
214 | // report success | |
215 | return TRUE; | |
216 | } | |
217 | ||
218 | //---------------------------------------------------------------------- | |
219 | // MyTextCtrl | |
220 | //---------------------------------------------------------------------- | |
221 | ||
222 | BEGIN_EVENT_TABLE(MyTextCtrl, wxTextCtrl) | |
223 | EVT_KEY_DOWN(MyTextCtrl::OnKeyDown) | |
224 | EVT_KEY_UP(MyTextCtrl::OnKeyUp) | |
225 | EVT_CHAR(MyTextCtrl::OnChar) | |
226 | END_EVENT_TABLE() | |
227 | ||
228 | void MyTextCtrl::LogEvent(const wxChar *name, wxKeyEvent& event) const | |
229 | { | |
230 | wxString key; | |
231 | long keycode = event.KeyCode(); | |
02e8b87f RR |
232 | { |
233 | switch ( keycode ) | |
234 | { | |
235 | case WXK_BACK: key = "BACK"; break; | |
236 | case WXK_TAB: key = "TAB"; break; | |
237 | case WXK_RETURN: key = "RETURN"; break; | |
238 | case WXK_ESCAPE: key = "ESCAPE"; break; | |
239 | case WXK_SPACE: key = "SPACE"; break; | |
240 | case WXK_DELETE: key = "DELETE"; break; | |
241 | case WXK_START: key = "START"; break; | |
242 | case WXK_LBUTTON: key = "LBUTTON"; break; | |
243 | case WXK_RBUTTON: key = "RBUTTON"; break; | |
244 | case WXK_CANCEL: key = "CANCEL"; break; | |
245 | case WXK_MBUTTON: key = "MBUTTON"; break; | |
246 | case WXK_CLEAR: key = "CLEAR"; break; | |
247 | case WXK_SHIFT: key = "SHIFT"; break; | |
248 | case WXK_ALT: key = "ALT"; break; | |
249 | case WXK_CONTROL: key = "CONTROL"; break; | |
250 | case WXK_MENU: key = "MENU"; break; | |
251 | case WXK_PAUSE: key = "PAUSE"; break; | |
252 | case WXK_CAPITAL: key = "CAPITAL"; break; | |
253 | case WXK_PRIOR: key = "PRIOR"; break; | |
254 | case WXK_NEXT: key = "NEXT"; break; | |
255 | case WXK_END: key = "END"; break; | |
256 | case WXK_HOME: key = "HOME"; break; | |
257 | case WXK_LEFT: key = "LEFT"; break; | |
258 | case WXK_UP: key = "UP"; break; | |
259 | case WXK_RIGHT: key = "RIGHT"; break; | |
260 | case WXK_DOWN: key = "DOWN"; break; | |
261 | case WXK_SELECT: key = "SELECT"; break; | |
262 | case WXK_PRINT: key = "PRINT"; break; | |
263 | case WXK_EXECUTE: key = "EXECUTE"; break; | |
264 | case WXK_SNAPSHOT: key = "SNAPSHOT"; break; | |
265 | case WXK_INSERT: key = "INSERT"; break; | |
266 | case WXK_HELP: key = "HELP"; break; | |
267 | case WXK_NUMPAD0: key = "NUMPAD0"; break; | |
268 | case WXK_NUMPAD1: key = "NUMPAD1"; break; | |
269 | case WXK_NUMPAD2: key = "NUMPAD2"; break; | |
270 | case WXK_NUMPAD3: key = "NUMPAD3"; break; | |
271 | case WXK_NUMPAD4: key = "NUMPAD4"; break; | |
272 | case WXK_NUMPAD5: key = "NUMPAD5"; break; | |
273 | case WXK_NUMPAD6: key = "NUMPAD6"; break; | |
274 | case WXK_NUMPAD7: key = "NUMPAD7"; break; | |
275 | case WXK_NUMPAD8: key = "NUMPAD8"; break; | |
276 | case WXK_NUMPAD9: key = "NUMPAD9"; break; | |
277 | case WXK_MULTIPLY: key = "MULTIPLY"; break; | |
278 | case WXK_ADD: key = "ADD"; break; | |
279 | case WXK_SEPARATOR: key = "SEPARATOR"; break; | |
280 | case WXK_SUBTRACT: key = "SUBTRACT"; break; | |
281 | case WXK_DECIMAL: key = "DECIMAL"; break; | |
282 | case WXK_DIVIDE: key = "DIVIDE"; break; | |
283 | case WXK_F1: key = "F1"; break; | |
284 | case WXK_F2: key = "F2"; break; | |
285 | case WXK_F3: key = "F3"; break; | |
286 | case WXK_F4: key = "F4"; break; | |
287 | case WXK_F5: key = "F5"; break; | |
288 | case WXK_F6: key = "F6"; break; | |
289 | case WXK_F7: key = "F7"; break; | |
290 | case WXK_F8: key = "F8"; break; | |
291 | case WXK_F9: key = "F9"; break; | |
292 | case WXK_F10: key = "F10"; break; | |
293 | case WXK_F11: key = "F11"; break; | |
294 | case WXK_F12: key = "F12"; break; | |
295 | case WXK_F13: key = "F13"; break; | |
296 | case WXK_F14: key = "F14"; break; | |
297 | case WXK_F15: key = "F15"; break; | |
298 | case WXK_F16: key = "F16"; break; | |
299 | case WXK_F17: key = "F17"; break; | |
300 | case WXK_F18: key = "F18"; break; | |
301 | case WXK_F19: key = "F19"; break; | |
302 | case WXK_F20: key = "F20"; break; | |
303 | case WXK_F21: key = "F21"; break; | |
304 | case WXK_F22: key = "F22"; break; | |
305 | case WXK_F23: key = "F23"; break; | |
306 | case WXK_F24: key = "F24"; break; | |
307 | case WXK_NUMLOCK: key = "NUMLOCK"; break; | |
308 | case WXK_SCROLL: key = "SCROLL"; break; | |
309 | case WXK_PAGEUP: key = "PAGEUP"; break; | |
310 | case WXK_PAGEDOWN: key = "PAGEDOWN"; break; | |
311 | case WXK_NUMPAD_SPACE: key = "NUMPAD_SPACE"; break; | |
312 | case WXK_NUMPAD_TAB: key = "NUMPAD_TAB"; break; | |
313 | case WXK_NUMPAD_ENTER: key = "NUMPAD_ENTER"; break; | |
314 | case WXK_NUMPAD_F1: key = "NUMPAD_F1"; break; | |
315 | case WXK_NUMPAD_F2: key = "NUMPAD_F2"; break; | |
316 | case WXK_NUMPAD_F3: key = "NUMPAD_F3"; break; | |
317 | case WXK_NUMPAD_F4: key = "NUMPAD_F4"; break; | |
318 | case WXK_NUMPAD_HOME: key = "NUMPAD_HOME"; break; | |
319 | case WXK_NUMPAD_LEFT: key = "NUMPAD_LEFT"; break; | |
320 | case WXK_NUMPAD_UP: key = "NUMPAD_UP"; break; | |
321 | case WXK_NUMPAD_RIGHT: key = "NUMPAD_RIGHT"; break; | |
322 | case WXK_NUMPAD_DOWN: key = "NUMPAD_DOWN"; break; | |
323 | case WXK_NUMPAD_PRIOR: key = "NUMPAD_PRIOR"; break; | |
324 | case WXK_NUMPAD_PAGEUP: key = "NUMPAD_PAGEUP"; break; | |
325 | case WXK_NUMPAD_PAGEDOWN: key = "NUMPAD_PAGEDOWN"; break; | |
326 | case WXK_NUMPAD_END: key = "NUMPAD_END"; break; | |
327 | case WXK_NUMPAD_BEGIN: key = "NUMPAD_BEGIN"; break; | |
328 | case WXK_NUMPAD_INSERT: key = "NUMPAD_INSERT"; break; | |
329 | case WXK_NUMPAD_DELETE: key = "NUMPAD_DELETE"; break; | |
330 | case WXK_NUMPAD_EQUAL: key = "NUMPAD_EQUAL"; break; | |
331 | case WXK_NUMPAD_MULTIPLY: key = "NUMPAD_MULTIPLY"; break; | |
332 | case WXK_NUMPAD_ADD: key = "NUMPAD_ADD"; break; | |
333 | case WXK_NUMPAD_SEPARATOR: key = "NUMPAD_SEPARATOR"; break; | |
334 | case WXK_NUMPAD_SUBTRACT: key = "NUMPAD_SUBTRACT"; break; | |
335 | case WXK_NUMPAD_DECIMAL: key = "NUMPAD_DECIMAL"; break; | |
336 | ||
89124b1a RR |
337 | default: |
338 | { | |
339 | if ( wxIsprint((int)keycode) ) | |
340 | key.Printf( _T("'%c'") , (char)keycode); | |
341 | else | |
342 | key.Printf( _T("unknown (%ld)"), keycode); | |
343 | } | |
02e8b87f RR |
344 | } |
345 | } | |
346 | ||
347 | wxLogMessage( _T("%s event: %s (flags = %c%c%c%c)"), | |
348 | name, | |
349 | key.c_str(), | |
350 | GetChar( event.ControlDown(), _T('C') ), | |
351 | GetChar( event.AltDown(), _T('A') ), | |
352 | GetChar( event.ShiftDown(), _T('S') ), | |
353 | GetChar( event.MetaDown(), _T('M') ) ); | |
354 | ||
355 | } | |
356 | ||
357 | void MyTextCtrl::OnChar(wxKeyEvent& event) | |
358 | { | |
359 | LogEvent( _T("Char"), event); | |
360 | ||
b6a0d8a2 VZ |
361 | if ( event.KeyCode() == WXK_TAB ) |
362 | { | |
363 | WriteText("\t"); | |
364 | } | |
365 | else | |
366 | { | |
367 | event.Skip(); | |
368 | } | |
02e8b87f RR |
369 | } |
370 | ||
371 | void MyTextCtrl::OnKeyUp(wxKeyEvent& event) | |
372 | { | |
373 | LogEvent( _("Key up"), event); | |
374 | ||
375 | event.Skip(); | |
376 | } | |
377 | ||
378 | void MyTextCtrl::OnKeyDown(wxKeyEvent& event) | |
379 | { | |
380 | switch ( event.KeyCode() ) | |
381 | { | |
382 | case WXK_F1: | |
383 | // show current position and text length | |
384 | { | |
385 | long line, column, pos = GetInsertionPoint(); | |
386 | PositionToXY(pos, &column, &line); | |
387 | ||
388 | wxLogMessage( _T("Current position: %ld\n" | |
389 | "Current line, column: (%ld, %ld)\n" | |
390 | "Number of lines: %ld\n" | |
391 | "Current line length: %ld\n" | |
392 | "Total text length: %ld"), | |
393 | pos, | |
394 | line, column, | |
395 | GetNumberOfLines(), | |
396 | GetLineLength(line), | |
397 | GetLastPosition()); | |
398 | } | |
399 | break; | |
400 | ||
401 | case WXK_F2: | |
402 | // go to the end | |
403 | SetInsertionPointEnd(); | |
404 | break; | |
405 | ||
406 | case WXK_F3: | |
407 | // go to position 10 | |
408 | SetInsertionPoint(10); | |
409 | break; | |
410 | } | |
411 | ||
412 | LogEvent( _("Key down"), event); | |
413 | ||
414 | event.Skip(); | |
415 | } | |
416 | ||
417 | //---------------------------------------------------------------------- | |
418 | // MyPanel | |
419 | //---------------------------------------------------------------------- | |
420 | ||
89124b1a RR |
421 | BEGIN_EVENT_TABLE(MyPanel, wxPanel) |
422 | EVT_SIZE(MyPanel::OnSize) | |
423 | END_EVENT_TABLE() | |
424 | ||
02e8b87f RR |
425 | MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h ) |
426 | : wxPanel( frame, -1, wxPoint(x, y), wxSize(w, h) ) | |
427 | { | |
428 | m_log = new wxTextCtrl( this, -1, "This is the log window.\n", wxPoint(5,260), wxSize(630,100), wxTE_MULTILINE ); | |
429 | ||
89124b1a RR |
430 | wxLog *old_log = wxLog::SetActiveTarget( new wxLogTextCtrl( m_log ) ); |
431 | ||
432 | delete old_log; | |
02e8b87f RR |
433 | |
434 | // single line text controls | |
435 | ||
bb69661b | 436 | m_text = new MyTextCtrl( this, -1, "Single line.", |
02e8b87f RR |
437 | wxPoint(10,10), wxSize(140,-1), 0); |
438 | (*m_text) << " Appended."; | |
439 | m_text->SetInsertionPoint(0); | |
440 | m_text->WriteText( "Prepended. " ); | |
bb69661b VZ |
441 | |
442 | m_password = new MyTextCtrl( this, -1, "", | |
02e8b87f | 443 | wxPoint(10,50), wxSize(140,-1), wxTE_PASSWORD ); |
bb69661b VZ |
444 | |
445 | m_readonly = new MyTextCtrl( this, -1, "Read only", | |
02e8b87f | 446 | wxPoint(10,90), wxSize(140,-1), wxTE_READONLY ); |
bb69661b | 447 | |
02e8b87f RR |
448 | // multi line text controls |
449 | ||
bb69661b VZ |
450 | m_horizontal = new MyTextCtrl( this, -1, "Multiline text control with a horizontal scrollbar.", |
451 | wxPoint(10,170), wxSize(140,70), wxTE_MULTILINE | wxHSCROLL ); | |
ddc8c2e3 VZ |
452 | m_horizontal->SetFont(wxFont(18, wxSWISS, wxNORMAL, wxNORMAL, |
453 | FALSE, "", wxFONTENCODING_KOI8)); | |
bb69661b | 454 | |
5e3dad49 | 455 | m_multitext = new MyTextCtrl( this, -1, "Multi line.", |
02e8b87f RR |
456 | wxPoint(180,10), wxSize(240,70), wxTE_MULTILINE ); |
457 | (*m_multitext) << " Appended."; | |
458 | m_multitext->SetInsertionPoint(0); | |
459 | m_multitext->WriteText( "Prepended. " ); | |
bb69661b | 460 | |
02e8b87f RR |
461 | #if wxUSE_TOOLTIPS |
462 | m_multitext->SetToolTip("Press F1 here."); | |
463 | #endif | |
464 | ||
bb69661b | 465 | m_tab = new MyTextCtrl( this, -1, "Multiline, allow <TAB> processing.", |
02e8b87f RR |
466 | wxPoint(180,90), wxSize(240,70), wxTE_MULTILINE | wxTE_PROCESS_TAB ); |
467 | ||
b6a0d8a2 VZ |
468 | m_enter = new MyTextCtrl( this, -1, "Multiline, allow <ENTER> processing.", |
469 | wxPoint(180,170), wxSize(240,70), wxTE_MULTILINE); | |
02e8b87f RR |
470 | } |
471 | ||
89124b1a RR |
472 | void MyPanel::OnSize( wxSizeEvent &event ) |
473 | { | |
474 | wxSize client_area( GetClientSize() ); | |
475 | m_log->SetSize( 0, 260, client_area.x, client_area.y - 260 ); | |
476 | event.Skip(); | |
477 | } | |
478 | ||
b6a0d8a2 VZ |
479 | #if wxUSE_CLIPBOARD |
480 | void MyPanel::DoPasteFromClipboard() | |
02e8b87f | 481 | { |
7e2c43b8 RR |
482 | // On X11, we want to get the data from the primary selection instead |
483 | // of the normal clipboard (which isn't normal under X11 at all). This | |
484 | // call has no effect under MSW. | |
485 | wxTheClipboard->UsePrimarySelection(); | |
486 | ||
02e8b87f RR |
487 | if (!wxTheClipboard->Open()) |
488 | { | |
489 | *m_log << "Error opening the clipboard.\n"; | |
02e8b87f RR |
490 | return; |
491 | } | |
492 | else | |
493 | { | |
494 | *m_log << "Successfully opened the clipboard.\n"; | |
495 | } | |
496 | ||
497 | wxTextDataObject data; | |
498 | ||
499 | if (wxTheClipboard->IsSupported( data.GetFormat() )) | |
500 | { | |
501 | *m_log << "Clipboard supports requested format.\n"; | |
502 | ||
503 | if (wxTheClipboard->GetData( &data )) | |
504 | { | |
505 | *m_log << "Successfully retrieved data from the clipboard.\n"; | |
506 | *m_multitext << data.GetText() << "\n"; | |
507 | } | |
508 | else | |
509 | { | |
510 | *m_log << "Error getting data from the clipboard.\n"; | |
511 | } | |
512 | } | |
513 | else | |
514 | { | |
515 | *m_log << "Clipboard doesn't support requested format.\n"; | |
516 | } | |
517 | ||
518 | wxTheClipboard->Close(); | |
519 | ||
520 | *m_log << "Closed the clipboard.\n"; | |
02e8b87f RR |
521 | } |
522 | ||
b6a0d8a2 | 523 | void MyPanel::DoCopyToClipboard() |
02e8b87f | 524 | { |
02e8b87f RR |
525 | wxString text( m_multitext->GetLineText(0) ); |
526 | ||
527 | if (text.IsEmpty()) | |
528 | { | |
529 | *m_log << "No text to copy.\n"; | |
530 | ||
531 | return; | |
532 | } | |
533 | ||
534 | if (!wxTheClipboard->Open()) | |
535 | { | |
536 | *m_log << "Error opening the clipboard.\n"; | |
537 | ||
538 | return; | |
539 | } | |
540 | else | |
541 | { | |
542 | *m_log << "Successfully opened the clipboard.\n"; | |
543 | } | |
544 | ||
545 | wxTextDataObject *data = new wxTextDataObject( text ); | |
546 | ||
547 | if (!wxTheClipboard->SetData( data )) | |
548 | { | |
549 | *m_log << "Error while copying to the clipboard.\n"; | |
550 | } | |
551 | else | |
552 | { | |
553 | *m_log << "Successfully copied data to the clipboard.\n"; | |
554 | } | |
555 | ||
556 | wxTheClipboard->Close(); | |
557 | ||
558 | *m_log << "Closed the clipboard.\n"; | |
02e8b87f RR |
559 | } |
560 | ||
b6a0d8a2 VZ |
561 | #endif // wxUSE_CLIPBOARD |
562 | ||
563 | void MyPanel::DoMoveToEndOfText() | |
02e8b87f RR |
564 | { |
565 | m_multitext->SetInsertionPointEnd(); | |
566 | m_multitext->SetFocus(); | |
567 | } | |
568 | ||
b6a0d8a2 | 569 | void MyPanel::DoMoveToEndOfEntry() |
02e8b87f RR |
570 | { |
571 | m_text->SetInsertionPointEnd(); | |
572 | m_text->SetFocus(); | |
573 | } | |
574 | ||
575 | //---------------------------------------------------------------------- | |
576 | // MyFrame | |
577 | //---------------------------------------------------------------------- | |
578 | ||
579 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
b6a0d8a2 VZ |
580 | EVT_MENU(TEXT_QUIT, MyFrame::OnQuit) |
581 | EVT_MENU(TEXT_ABOUT, MyFrame::OnAbout) | |
582 | EVT_MENU(TEXT_LOAD, MyFrame::OnFileLoad) | |
583 | ||
02e8b87f | 584 | #if wxUSE_TOOLTIPS |
b6a0d8a2 VZ |
585 | EVT_MENU(TEXT_TOOLTIPS_SETDELAY, MyFrame::OnSetTooltipDelay) |
586 | EVT_MENU(TEXT_TOOLTIPS_ENABLE, MyFrame::OnToggleTooltips) | |
02e8b87f | 587 | #endif // wxUSE_TOOLTIPS |
b6a0d8a2 | 588 | |
ee31c392 | 589 | #if wxUSE_CLIPBOARD |
b6a0d8a2 VZ |
590 | EVT_MENU(TEXT_CLIPBOARD_PASTE, MyFrame::OnPasteFromClipboard) |
591 | EVT_MENU(TEXT_CLIPBOARD_COPY, MyFrame::OnCopyToClipboard) | |
ee31c392 VZ |
592 | #endif // wxUSE_CLIPBOARD |
593 | ||
b6a0d8a2 VZ |
594 | EVT_MENU(TEXT_MOVE_ENDTEXT, MyFrame::OnMoveToEndOfText) |
595 | EVT_MENU(TEXT_MOVE_ENDENTRY, MyFrame::OnMoveToEndOfEntry) | |
596 | ||
597 | EVT_IDLE(MyFrame::OnIdle) | |
02e8b87f RR |
598 | END_EVENT_TABLE() |
599 | ||
b6a0d8a2 VZ |
600 | MyFrame::MyFrame(wxFrame *frame, const char *title, int x, int y, int w, int h) |
601 | : wxFrame(frame, -1, title, wxPoint(x, y), wxSize(w, h) ) | |
02e8b87f RR |
602 | { |
603 | CreateStatusBar(2); | |
604 | ||
b6a0d8a2 | 605 | m_panel = new MyPanel( this, 10, 10, 300, 100 ); |
02e8b87f RR |
606 | } |
607 | ||
608 | void MyFrame::OnQuit (wxCommandEvent& WXUNUSED(event) ) | |
609 | { | |
610 | Close(TRUE); | |
611 | } | |
612 | ||
613 | void MyFrame::OnAbout( wxCommandEvent& WXUNUSED(event) ) | |
614 | { | |
615 | wxBeginBusyCursor(); | |
616 | ||
b6a0d8a2 VZ |
617 | wxMessageDialog dialog(this, |
618 | "This is a text control sample. It demonstrates the many different\n" | |
619 | "text control styles, the use of the clipboard, setting and handling\n" | |
620 | "tooltips and intercepting key and char events.\n" | |
621 | "\n" | |
622 | "Copyright (c) 1999, Robert Roebling, Julian Smart, Vadim Zeitlin", | |
623 | "About Text Controls", | |
624 | wxOK | wxICON_INFORMATION); | |
625 | ||
02e8b87f RR |
626 | dialog.ShowModal(); |
627 | ||
628 | wxEndBusyCursor(); | |
629 | } | |
630 | ||
631 | #if wxUSE_TOOLTIPS | |
632 | void MyFrame::OnSetTooltipDelay(wxCommandEvent& event) | |
633 | { | |
634 | static long s_delay = 5000; | |
635 | ||
636 | wxString delay; | |
637 | delay.Printf( _T("%ld"), s_delay); | |
638 | ||
639 | delay = wxGetTextFromUser("Enter delay (in milliseconds)", | |
bb69661b VZ |
640 | "Set tooltip delay", |
641 | delay, | |
642 | this); | |
02e8b87f RR |
643 | if ( !delay ) |
644 | return; // cancelled | |
645 | ||
646 | wxSscanf(delay, _T("%ld"), &s_delay); | |
647 | ||
648 | wxToolTip::SetDelay(s_delay); | |
649 | ||
650 | wxLogStatus(this, _T("Tooltip delay set to %ld milliseconds"), s_delay); | |
651 | } | |
652 | ||
653 | void MyFrame::OnToggleTooltips(wxCommandEvent& event) | |
654 | { | |
655 | static bool s_enabled = TRUE; | |
656 | ||
657 | s_enabled = !s_enabled; | |
658 | ||
659 | wxToolTip::Enable(s_enabled); | |
660 | ||
661 | wxLogStatus(this, _T("Tooltips %sabled"), s_enabled ? _T("en") : _T("dis") ); | |
662 | } | |
663 | #endif // tooltips | |
664 | ||
b6a0d8a2 VZ |
665 | void MyFrame::OnFileLoad(wxCommandEvent& event) |
666 | { | |
ddc8c2e3 | 667 | if ( m_panel->m_multitext->LoadFile("text.rc") ) |
5e3dad49 VZ |
668 | wxLogStatus(this, _T("Successfully loaded file")); |
669 | else | |
670 | wxLogStatus(this, _T("Couldn't load the file")); | |
b6a0d8a2 VZ |
671 | } |
672 | ||
02e8b87f RR |
673 | void MyFrame::OnIdle( wxIdleEvent& event ) |
674 | { | |
675 | // track the window which has the focus in the status bar | |
676 | static wxWindow *s_windowFocus = (wxWindow *)NULL; | |
677 | wxWindow *focus = wxWindow::FindFocus(); | |
678 | if ( focus && (focus != s_windowFocus) ) | |
679 | { | |
680 | s_windowFocus = focus; | |
681 | ||
682 | wxString msg; | |
683 | msg.Printf( | |
684 | #ifdef __WXMSW__ | |
685 | _T("Focus: wxWindow = %p, HWND = %08x"), | |
686 | #else | |
687 | _T("Focus: wxWindow = %p"), | |
688 | #endif | |
689 | s_windowFocus | |
690 | #ifdef __WXMSW__ | |
691 | , s_windowFocus->GetHWND() | |
692 | #endif | |
693 | ); | |
694 | ||
695 | SetStatusText(msg); | |
696 | } | |
697 | event.Skip(); | |
698 | } |