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