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