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