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