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