]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
Various compile fixes.
[wxWidgets.git] / src / msw / textctrl.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
bfbd6dc1 2// Name: msw/textctrl.cpp
2bda0e17
KB
3// Purpose: wxTextCtrl
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
c085e333 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
a1b82138
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
2bda0e17 16#ifdef __GNUG__
a1b82138 17 #pragma implementation "textctrl.h"
2bda0e17
KB
18#endif
19
a1b82138
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
2bda0e17
KB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
a1b82138 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_TEXTCTRL
32
2bda0e17 33#ifndef WX_PRECOMP
a1b82138
VZ
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
36 #include "wx/brush.h"
37 #include "wx/utils.h"
381dd4bf 38 #include "wx/intl.h"
a1b82138 39 #include "wx/log.h"
381dd4bf 40 #include "wx/app.h"
2bda0e17
KB
41#endif
42
f6bcfd97
BP
43#include "wx/module.h"
44
47d67540 45#if wxUSE_CLIPBOARD
a1b82138 46 #include "wx/clipbrd.h"
2bda0e17
KB
47#endif
48
a1b82138
VZ
49#include "wx/textfile.h"
50
51#include <windowsx.h>
52
2bda0e17
KB
53#include "wx/msw/private.h"
54
a1b82138 55#include <string.h>
2bda0e17 56#include <stdlib.h>
a1b82138 57#include <sys/types.h>
fbc535ff 58
ae090fdb 59#if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
cd471848 60 #include <richedit.h>
2bda0e17
KB
61#endif
62
e80591b9
VZ
63// old mingw32 doesn't define this
64#ifndef CFM_CHARSET
65 #define CFM_CHARSET 0x08000000
66#endif // CFM_CHARSET
67
784164e1
VZ
68#ifndef CFM_BACKCOLOR
69 #define CFM_BACKCOLOR 0x04000000
70#endif
71
733e8cf3
VZ
72// cygwin does not have these defined for richedit
73#ifndef ENM_LINK
74 #define ENM_LINK 0x04000000
75#endif
76
77#ifndef EM_AUTOURLDETECT
78 #define EM_AUTOURLDETECT (WM_USER + 91)
79#endif
80
81#ifndef EN_LINK
82 #define EN_LINK 0x070b
83
84 typedef struct _enlink
85 {
86 NMHDR nmhdr;
87 UINT msg;
88 WPARAM wParam;
89 LPARAM lParam;
90 CHARRANGE chrg;
91 } ENLINK;
92#endif // ENLINK
93
7a25a27c
VZ
94#ifndef SF_UNICODE
95 #define SF_UNICODE 0x0010
96#endif
97
a5aa8086
VZ
98// Watcom C++ doesn't define this
99#ifndef SCF_ALL
100#define SCF_ALL 0x0004
101#endif
102
aac7e7fe
VZ
103// ----------------------------------------------------------------------------
104// private functions
105// ----------------------------------------------------------------------------
106
107#if wxUSE_RICHEDIT
108
109DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb);
110
111#endif // wxUSE_RICHEDIT
112
b12915c1
VZ
113// ----------------------------------------------------------------------------
114// private classes
115// ----------------------------------------------------------------------------
116
117#if wxUSE_RICHEDIT
118
a5aa8086 119// this module initializes RichEdit DLL(s) if needed
b12915c1
VZ
120class wxRichEditModule : public wxModule
121{
122public:
123 virtual bool OnInit();
124 virtual void OnExit();
125
b12915c1
VZ
126 // load the richedit DLL of at least of required version
127 static bool Load(int version = 1);
128
129private:
a5aa8086
VZ
130 // the handles to richedit 1.0 and 2.0 (or 3.0) DLLs
131 static HINSTANCE ms_hRichEdit[2];
b12915c1
VZ
132
133 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
134};
135
a5aa8086 136HINSTANCE wxRichEditModule::ms_hRichEdit[2] = { NULL, NULL };
b12915c1
VZ
137
138IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
139
140#endif // wxUSE_RICHEDIT
e702ff0f 141
a1b82138
VZ
142// ----------------------------------------------------------------------------
143// event tables and other macros
144// ----------------------------------------------------------------------------
145
2bda0e17
KB
146IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
147
148BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
a1b82138
VZ
149 EVT_CHAR(wxTextCtrl::OnChar)
150 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
151
152 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
153 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
154 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
155 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
156 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
157
158 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
159 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
160 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
161 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
162 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
f6bcfd97
BP
163#ifdef __WIN16__
164 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
165#endif
2bda0e17 166END_EVENT_TABLE()
e702ff0f 167
a1b82138
VZ
168// ============================================================================
169// implementation
170// ============================================================================
171
172// ----------------------------------------------------------------------------
173// creation
174// ----------------------------------------------------------------------------
175
aac7e7fe 176void wxTextCtrl::Init()
2bda0e17 177{
cd471848 178#if wxUSE_RICHEDIT
aac7e7fe 179 m_verRichEdit = 0;
cd471848 180#endif
2bda0e17
KB
181}
182
debe6624 183bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
184 const wxString& value,
185 const wxPoint& pos,
a1b82138
VZ
186 const wxSize& size,
187 long style,
c085e333
VZ
188 const wxValidator& validator,
189 const wxString& name)
2bda0e17 190{
a1b82138 191 // base initialization
8d99be5f 192 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
a1b82138 193 return FALSE;
2bda0e17 194
a1b82138
VZ
195 if ( parent )
196 parent->AddChild(this);
2bda0e17 197
a1b82138
VZ
198 // translate wxWin style flags to MSW ones, checking for consistency while
199 // doing it
7da982c4 200 long msStyle = ES_LEFT | WS_TABSTOP;
b0766406
JS
201
202 if ( m_windowStyle & wxCLIP_SIBLINGS )
203 msStyle |= WS_CLIPSIBLINGS;
204
a1b82138
VZ
205 if ( m_windowStyle & wxTE_MULTILINE )
206 {
207 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
f6bcfd97 208 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
5fb9fcfc 209
b70ababc
JS
210 msStyle |= ES_MULTILINE | ES_WANTRETURN;
211 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
212 msStyle |= WS_VSCROLL;
a1b82138
VZ
213 m_windowStyle |= wxTE_PROCESS_ENTER;
214 }
5a8f04e3
VZ
215 else // !multiline
216 {
217 // there is really no reason to not have this style for single line
218 // text controls
a1b82138 219 msStyle |= ES_AUTOHSCROLL;
5a8f04e3 220 }
2bda0e17 221
5a8f04e3
VZ
222 if ( m_windowStyle & wxHSCROLL )
223 msStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
2c738dd8 224
5a8f04e3 225 if ( m_windowStyle & wxTE_READONLY )
a1b82138 226 msStyle |= ES_READONLY;
2bda0e17 227
5a8f04e3 228 if ( m_windowStyle & wxTE_PASSWORD )
a1b82138 229 msStyle |= ES_PASSWORD;
2bda0e17 230
5a8f04e3
VZ
231 if ( m_windowStyle & wxTE_AUTO_SCROLL )
232 msStyle |= ES_AUTOHSCROLL;
cfdd3a98 233
5a8f04e3
VZ
234 if ( m_windowStyle & wxTE_NOHIDESEL )
235 msStyle |= ES_NOHIDESEL;
cfdd3a98 236
a1b82138 237 // do create the control - either an EDIT or RICHEDIT
b12915c1 238 wxString windowClass = wxT("EDIT");
cd471848 239
57c208c5 240#if wxUSE_RICHEDIT
a5aa8086
VZ
241 if ( m_windowStyle & wxTE_AUTO_URL )
242 {
243 // automatic URL detection only works in RichEdit 2.0+
244 m_windowStyle |= wxTE_RICH2;
245 }
246
247 if ( m_windowStyle & wxTE_RICH2 )
248 {
249 // using richedit 2.0 implies using wxTE_RICH
250 m_windowStyle |= wxTE_RICH;
251 }
252
253 // we need to load the richedit DLL before creating the rich edit control
c49245f8 254 if ( m_windowStyle & wxTE_RICH )
a1b82138 255 {
a5aa8086
VZ
256 static bool s_errorGiven = FALSE;// MT-FIXME
257
258 // Which version do we need? Use 1.0 by default because it is much more
259 // like the the standard EDIT or 2.0 if explicitly requested, but use
260 // only 2.0 in Unicode mode as 1.0 doesn't support Unicode at all
261 //
262 // TODO: RichEdit 3.0 is apparently capable of emulating RichEdit 1.0
263 // (and thus EDIT) much better than RichEdit 2.0 so we probably
264 // should use 3.0 if available as it is the best of both worlds -
265 // but as I can't test it right now I don't do it (VZ)
266#if wxUSE_UNICODE
267 const int verRichEdit = 2;
268#else // !wxUSE_UNICODE
269 int verRichEdit = m_windowStyle & wxTE_RICH2 ? 2 : 1;
270#endif // wxUSE_UNICODE/!wxUSE_UNICODE
0d0512bd
VZ
271
272 // only give the error msg once if the DLL can't be loaded
273 if ( !s_errorGiven )
274 {
a5aa8086
VZ
275 // try to load the RichEdit DLL (will do nothing if already done)
276 if ( !wxRichEditModule::Load(verRichEdit) )
0d0512bd 277 {
a5aa8086
VZ
278#if !wxUSE_UNICODE
279 // try another version?
280 verRichEdit = 3 - verRichEdit; // 1 <-> 2
281
282 if ( !wxRichEditModule::Load(verRichEdit) )
283#endif // wxUSE_UNICODE
284 {
285 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
0d0512bd 286
a5aa8086
VZ
287 s_errorGiven = TRUE;
288 }
0d0512bd
VZ
289 }
290 }
291
a5aa8086 292 // have we managed to load any richedit version?
aac7e7fe 293 if ( !s_errorGiven )
0d0512bd
VZ
294 {
295 msStyle |= ES_AUTOVSCROLL;
aac7e7fe 296
a5aa8086 297 m_verRichEdit = verRichEdit;
aac7e7fe 298 if ( m_verRichEdit == 1 )
b12915c1
VZ
299 {
300 windowClass = wxT("RICHEDIT");
301 }
302 else
303 {
304#ifndef RICHEDIT_CLASS
305 wxString RICHEDIT_CLASS;
aac7e7fe 306 RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), m_verRichEdit);
5fb2f4ba 307#if wxUSE_UNICODE
b12915c1
VZ
308 RICHEDIT_CLASS += _T('W');
309#else // ANSI
310 RICHEDIT_CLASS += _T('A');
311#endif // Unicode/ANSI
312#endif // !RICHEDIT_CLASS
313
314 windowClass = RICHEDIT_CLASS;
315 }
0d0512bd 316 }
a1b82138 317 }
b12915c1 318#endif // wxUSE_RICHEDIT
2bda0e17 319
c8b204e6
VZ
320 // we need to turn '\n's into "\r\n"s for the multiline controls
321 wxString valueWin;
322 if ( m_windowStyle & wxTE_MULTILINE )
323 {
324 valueWin = wxTextFile::Translate(value, wxTextFileType_Dos);
325 }
326 else // single line
327 {
328 valueWin = value;
329 }
330
331 if ( !MSWCreateControl(windowClass, msStyle, pos, size, valueWin) )
7da982c4 332 return FALSE;
2bda0e17 333
a756f210 334 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
86f14582 335
57c208c5 336#if wxUSE_RICHEDIT
aac7e7fe 337 if ( IsRich() )
a1b82138 338 {
c57e3339
VZ
339 // have to enable events manually
340 LPARAM mask = ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE;
341
342 if ( m_windowStyle & wxTE_AUTO_URL )
343 {
344 mask |= ENM_LINK;
345
346 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0);
347 }
348
349 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask);
a1b82138 350 }
c57e3339 351#endif // wxUSE_RICHEDIT
2bda0e17 352
a1b82138 353 return TRUE;
2bda0e17
KB
354}
355
356// Make sure the window style (etc.) reflects the HWND style (roughly)
cd471848 357void wxTextCtrl::AdoptAttributesFromHWND()
2bda0e17 358{
aac7e7fe 359 wxWindow::AdoptAttributesFromHWND();
2bda0e17 360
aac7e7fe
VZ
361 HWND hWnd = GetHwnd();
362 long style = ::GetWindowLong(hWnd, GWL_STYLE);
2bda0e17 363
aac7e7fe 364 // retrieve the style to see whether this is an edit or richedit ctrl
cd471848 365#if wxUSE_RICHEDIT
aac7e7fe 366 wxString classname = wxGetWindowClass(GetHWND());
2bda0e17 367
aac7e7fe
VZ
368 if ( classname.IsSameAs(_T("EDIT"), FALSE /* no case */) )
369 {
370 m_verRichEdit = 0;
371 }
372 else // rich edit?
373 {
374 wxChar c;
375 if ( wxSscanf(classname, _T("RichEdit%d0%c"), &m_verRichEdit, &c) != 2 )
376 {
377 wxLogDebug(_T("Unknown edit control '%s'."), classname.c_str());
2bda0e17 378
aac7e7fe
VZ
379 m_verRichEdit = 0;
380 }
381 }
a1b82138 382#endif // wxUSE_RICHEDIT
c085e333 383
aac7e7fe
VZ
384 if (style & ES_MULTILINE)
385 m_windowStyle |= wxTE_MULTILINE;
386 if (style & ES_PASSWORD)
387 m_windowStyle |= wxTE_PASSWORD;
388 if (style & ES_READONLY)
389 m_windowStyle |= wxTE_READONLY;
390 if (style & ES_WANTRETURN)
391 m_windowStyle |= wxTE_PROCESS_ENTER;
2bda0e17
KB
392}
393
a1b82138
VZ
394// ----------------------------------------------------------------------------
395// set/get the controls text
396// ----------------------------------------------------------------------------
397
cd471848 398wxString wxTextCtrl::GetValue() const
2bda0e17 399{
a5aa8086
VZ
400 // range 0..-1 is special for GetRange() and means to retrieve all text
401 return GetRange(0, -1);
402}
403
404wxString wxTextCtrl::GetRange(long from, long to) const
405{
406 wxString str;
407
408 if ( from >= to && to != -1 )
409 {
410 // nothing to retrieve
411 return str;
412 }
413
b12915c1 414#if wxUSE_RICHEDIT
aac7e7fe 415 if ( IsRich() )
b12915c1 416 {
3988b155 417 int len = GetWindowTextLength(GetHwnd());
a5aa8086 418 if ( len > from )
3988b155
VZ
419 {
420 // alloc one extra WORD as needed by the control
421 wxChar *p = str.GetWriteBuf(++len);
b12915c1 422
3988b155 423 TEXTRANGE textRange;
a5aa8086
VZ
424 textRange.chrg.cpMin = from;
425 textRange.chrg.cpMax = to == -1 ? len : to;
3988b155 426 textRange.lpstrText = p;
b12915c1 427
3988b155 428 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
b12915c1 429
a5aa8086 430 if ( m_verRichEdit > 1 )
3988b155 431 {
a5aa8086
VZ
432 // RichEdit 2.0 uses just CR ('\r') for the newlines which is
433 // neither Unix nor Windows style - convert it to something
434 // reasonable
435 for ( ; *p; p++ )
436 {
437 if ( *p == _T('\r') )
438 *p = _T('\n');
439 }
3988b155
VZ
440 }
441
442 str.UngetWriteBuf();
a5aa8086
VZ
443
444 if ( m_verRichEdit == 1 )
445 {
446 // convert to the canonical form - see comment below
447 str = wxTextFile::Translate(str, wxTextFileType_Unix);
448 }
3988b155
VZ
449 }
450 //else: no text at all, leave the string empty
b12915c1 451 }
a5aa8086 452 else
b12915c1 453#endif // wxUSE_RICHEDIT
a5aa8086
VZ
454 {
455 // retrieve all text
456 str = wxGetWindowText(GetHWND());
b12915c1 457
a5aa8086
VZ
458 // need only a range?
459 if ( from < to )
460 {
461 str = str.Mid(from, to - from);
462 }
463
464 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
465 // canonical one (same one as above) for consistency with the other kinds
466 // of controls and, more importantly, with the other ports
467 str = wxTextFile::Translate(str, wxTextFileType_Unix);
468 }
b12915c1 469
a5aa8086 470 return str;
2bda0e17
KB
471}
472
473void wxTextCtrl::SetValue(const wxString& value)
474{
b12915c1
VZ
475 // if the text is long enough, it's faster to just set it instead of first
476 // comparing it with the old one (chances are that it will be different
477 // anyhow, this comparison is there to avoid flicker for small single-line
478 // edit controls mostly)
479 if ( (value.length() > 0x400) || (value != GetValue()) )
07cf98cb 480 {
79d26b32 481 DoWriteText(value, FALSE /* not selection only */);
af01f1ba 482
104d7404
VZ
483 // mark the control as being not dirty - we changed its text, not the
484 // user
485 DiscardEdits();
486
af01f1ba
VZ
487 // for compatibility, don't move the cursor when doing SetValue()
488 SetInsertionPoint(0);
aac7e7fe
VZ
489 }
490}
a1b82138 491
0b8e5844 492#if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
f6bcfd97 493
aac7e7fe
VZ
494DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb)
495{
496 *pcb = 0;
f6bcfd97 497
aac7e7fe
VZ
498 wchar_t *wbuf = (wchar_t *)buf;
499 const wchar_t *wpc = *(const wchar_t **)dwCookie;
500 while ( cb && *wpc )
501 {
502 *wbuf++ = *wpc++;
503
504 cb -= sizeof(wchar_t);
505 (*pcb) += sizeof(wchar_t);
07cf98cb 506 }
aac7e7fe
VZ
507
508 *(const wchar_t **)dwCookie = wpc;
509
510 return 0;
2bda0e17
KB
511}
512
373658eb 513extern long wxEncodingToCodepage(wxFontEncoding encoding); // from utils.cpp
aac7e7fe 514
0b8e5844 515#if wxUSE_UNICODE_MSLU
79d26b32
VZ
516bool wxTextCtrl::StreamIn(const wxString& value,
517 wxFontEncoding WXUNUSED(encoding),
518 bool selectionOnly)
0b8e5844
VS
519{
520 const wchar_t *wpc = value.c_str();
79d26b32
VZ
521#else // !wxUSE_UNICODE_MSLU
522bool wxTextCtrl::StreamIn(const wxString& value,
523 wxFontEncoding encoding,
524 bool selectionOnly)
aac7e7fe
VZ
525{
526 // we have to use EM_STREAMIN to force richedit control 2.0+ to show any
527 // text in the non default charset - otherwise it thinks it knows better
528 // than we do and always shows it in the default one
529
530 // first get the Windows code page for this encoding
531 long codepage = wxEncodingToCodepage(encoding);
532 if ( codepage == -1 )
533 {
534 // unknown encoding
535 return FALSE;
536 }
537
538 // next translate to Unicode using this code page
539 int len = ::MultiByteToWideChar(codepage, 0, value, -1, NULL, 0);
855d6be7
VZ
540
541#if wxUSE_WCHAR_T
aac7e7fe 542 wxWCharBuffer wchBuf(len);
855d6be7
VZ
543#else
544 wchar_t *wchBuf = (wchar_t *)malloc((len + 1)*sizeof(wchar_t));
545#endif
546
aac7e7fe 547 if ( !::MultiByteToWideChar(codepage, 0, value, -1,
855d6be7 548 (wchar_t *)(const wchar_t *)wchBuf, len) )
aac7e7fe
VZ
549 {
550 wxLogLastError(_T("MultiByteToWideChar"));
551 }
552
553 // finally, stream it in the control
554 const wchar_t *wpc = wchBuf;
0b8e5844 555#endif // wxUSE_UNICODE_MSLU
aac7e7fe
VZ
556
557 EDITSTREAM eds;
558 wxZeroMemory(eds);
559 eds.dwCookie = (DWORD)&wpc;
7a25a27c
VZ
560 // the cast below is needed for broken (very) old mingw32 headers
561 eds.pfnCallback = (EDITSTREAMCALLBACK)wxRichEditStreamIn;
92209a39 562
aac7e7fe 563 if ( !::SendMessage(GetHwnd(), EM_STREAMIN,
79d26b32
VZ
564 SF_TEXT |
565 SF_UNICODE |
566 (selectionOnly ? SFF_SELECTION : 0),
aac7e7fe
VZ
567 (LPARAM)&eds) || eds.dwError )
568 {
569 wxLogLastError(_T("EM_STREAMIN"));
aac7e7fe
VZ
570 }
571
855d6be7
VZ
572#if !wxUSE_WCHAR_T
573 free(wchBuf);
574#endif // !wxUSE_WCHAR_T
575
aac7e7fe
VZ
576 return TRUE;
577}
578
579#endif // wxUSE_RICHEDIT
580
a1b82138 581void wxTextCtrl::WriteText(const wxString& value)
79d26b32
VZ
582{
583 DoWriteText(value);
584}
585
586void wxTextCtrl::DoWriteText(const wxString& value, bool selectionOnly)
2bda0e17 587{
a5aa8086
VZ
588 wxString valueDos;
589 if ( m_windowStyle & wxTE_MULTILINE )
590 valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
591 else
592 valueDos = value;
2bda0e17 593
4bc1afd5 594#if wxUSE_RICHEDIT
aac7e7fe
VZ
595 // there are several complications with the rich edit controls here
596 bool done = FALSE;
597 if ( IsRich() )
4bc1afd5 598 {
aac7e7fe
VZ
599 // first, ensure that the new text will be in the default style
600 if ( !m_defaultStyle.IsDefault() )
601 {
602 long start, end;
603 GetSelection(&start, &end);
0b8e5844
VS
604 SetStyle(start, end, m_defaultStyle);
605 }
606
607#if wxUSE_UNICODE_MSLU
608 // RichEdit doesn't have Unicode version of EM_REPLACESEL on Win9x,
609 // but EM_STREAMIN works
610 if ( wxGetOsVersion() == wxWIN95 && GetRichVersion() > 1 )
611 {
79d26b32 612 done = StreamIn(valueDos, wxFONTENCODING_SYSTEM, selectionOnly);
aac7e7fe 613 }
0b8e5844 614#endif // wxUSE_UNICODE_MSLU
aac7e7fe 615
9d7de3c2 616#if !wxUSE_UNICODE
aac7e7fe
VZ
617 // next check if the text we're inserting must be shown in a non
618 // default charset -- this only works for RichEdit > 1.0
619 if ( GetRichVersion() > 1 )
620 {
621 wxFont font = m_defaultStyle.GetFont();
622 if ( !font.Ok() )
623 font = GetFont();
624
625 if ( font.Ok() )
626 {
627 wxFontEncoding encoding = font.GetEncoding();
628 if ( encoding != wxFONTENCODING_SYSTEM )
629 {
79d26b32 630 done = StreamIn(valueDos, encoding, selectionOnly);
aac7e7fe
VZ
631 }
632 }
633 }
9d7de3c2 634#endif // !wxUSE_UNICODE
4bc1afd5 635 }
4bc1afd5 636
aac7e7fe
VZ
637 if ( !done )
638#endif // wxUSE_RICHEDIT
639 {
79d26b32
VZ
640 if ( !selectionOnly )
641 {
642 SetSelection(-1, -1);
643 }
644
aac7e7fe
VZ
645 ::SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
646 }
2bda0e17 647
a1b82138 648 AdjustSpaceLimit();
2bda0e17
KB
649}
650
a1b82138
VZ
651void wxTextCtrl::AppendText(const wxString& text)
652{
653 SetInsertionPointEnd();
aac7e7fe 654
a1b82138
VZ
655 WriteText(text);
656}
657
658void wxTextCtrl::Clear()
659{
aac7e7fe 660 ::SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
661}
662
663// ----------------------------------------------------------------------------
2bda0e17 664// Clipboard operations
a1b82138
VZ
665// ----------------------------------------------------------------------------
666
cd471848 667void wxTextCtrl::Copy()
2bda0e17 668{
e702ff0f
JS
669 if (CanCopy())
670 {
a5aa8086 671 ::SendMessage(GetHwnd(), WM_COPY, 0, 0L);
e702ff0f 672 }
2bda0e17
KB
673}
674
cd471848 675void wxTextCtrl::Cut()
2bda0e17 676{
e702ff0f
JS
677 if (CanCut())
678 {
a5aa8086 679 ::SendMessage(GetHwnd(), WM_CUT, 0, 0L);
e702ff0f 680 }
2bda0e17
KB
681}
682
cd471848 683void wxTextCtrl::Paste()
2bda0e17 684{
e702ff0f
JS
685 if (CanPaste())
686 {
a5aa8086 687 ::SendMessage(GetHwnd(), WM_PASTE, 0, 0L);
e702ff0f 688 }
2bda0e17
KB
689}
690
a1b82138
VZ
691bool wxTextCtrl::CanCopy() const
692{
693 // Can copy if there's a selection
694 long from, to;
a5aa8086
VZ
695 GetSelection(&from, &to);
696 return from != to;
a1b82138
VZ
697}
698
699bool wxTextCtrl::CanCut() const
700{
a5aa8086 701 return CanCopy() && IsEditable();
a1b82138
VZ
702}
703
704bool wxTextCtrl::CanPaste() const
705{
aac7e7fe
VZ
706 if ( !IsEditable() )
707 return FALSE;
708
a1b82138 709#if wxUSE_RICHEDIT
aac7e7fe 710 if ( IsRich() )
a1b82138 711 {
aac7e7fe
VZ
712 UINT cf = 0; // 0 == any format
713
714 return ::SendMessage(GetHwnd(), EM_CANPASTE, cf, 0) != 0;
a1b82138 715 }
aac7e7fe 716#endif // wxUSE_RICHEDIT
a1b82138
VZ
717
718 // Standard edit control: check for straight text on clipboard
aac7e7fe
VZ
719 if ( !::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
720 return FALSE;
721
722 bool isTextAvailable = ::IsClipboardFormatAvailable(CF_TEXT) != 0;
723 ::CloseClipboard();
a1b82138
VZ
724
725 return isTextAvailable;
726}
727
728// ----------------------------------------------------------------------------
729// Accessors
730// ----------------------------------------------------------------------------
731
debe6624 732void wxTextCtrl::SetEditable(bool editable)
2bda0e17 733{
a1b82138
VZ
734 HWND hWnd = GetHwnd();
735 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
736}
737
debe6624 738void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 739{
a5aa8086 740 DoSetSelection(pos, pos);
2bda0e17
KB
741}
742
cd471848 743void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 744{
a5aa8086
VZ
745 long pos;
746
747#if wxUSE_RICHEDIT
748 if ( m_verRichEdit == 1 )
749 {
750 // we don't have to waste time calling GetLastPosition() in this case
751 pos = -1;
752 }
753 else // !RichEdit 1.0
754#endif // wxUSE_RICHEDIT
755 {
756 pos = GetLastPosition();
757 }
758
a1b82138 759 SetInsertionPoint(pos);
2bda0e17
KB
760}
761
cd471848 762long wxTextCtrl::GetInsertionPoint() const
2bda0e17 763{
57c208c5 764#if wxUSE_RICHEDIT
aac7e7fe 765 if ( IsRich() )
a1b82138
VZ
766 {
767 CHARRANGE range;
768 range.cpMin = 0;
769 range.cpMax = 0;
770 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
771 return range.cpMin;
772 }
aac7e7fe 773#endif // wxUSE_RICHEDIT
2bda0e17 774
a1b82138
VZ
775 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
776 return Pos & 0xFFFF;
2bda0e17
KB
777}
778
cd471848 779long wxTextCtrl::GetLastPosition() const
2bda0e17 780{
a5aa8086
VZ
781 int numLines = GetNumberOfLines();
782 long posStartLastLine = XYToPosition(0, numLines - 1);
39136494 783
a5aa8086 784 long lenLastLine = GetLengthOfLineContainingPos(posStartLastLine);
2bda0e17 785
a5aa8086 786 return posStartLastLine + lenLastLine;
2bda0e17
KB
787}
788
a1b82138
VZ
789// If the return values from and to are the same, there is no
790// selection.
791void wxTextCtrl::GetSelection(long* from, long* to) const
792{
793#if wxUSE_RICHEDIT
aac7e7fe 794 if ( IsRich() )
a1b82138
VZ
795 {
796 CHARRANGE charRange;
aac7e7fe 797 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &charRange);
a1b82138
VZ
798
799 *from = charRange.cpMin;
800 *to = charRange.cpMax;
a1b82138 801 }
4bc1afd5 802 else
aac7e7fe 803#endif // !wxUSE_RICHEDIT
4bc1afd5
VZ
804 {
805 DWORD dwStart, dwEnd;
aac7e7fe 806 ::SendMessage(GetHwnd(), EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
a1b82138 807
4bc1afd5
VZ
808 *from = dwStart;
809 *to = dwEnd;
810 }
a1b82138
VZ
811}
812
813bool wxTextCtrl::IsEditable() const
814{
815 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
816
a5aa8086 817 return (style & ES_READONLY) == 0;
a1b82138
VZ
818}
819
820// ----------------------------------------------------------------------------
aac7e7fe 821// selection
a1b82138
VZ
822// ----------------------------------------------------------------------------
823
aac7e7fe 824void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 825{
aac7e7fe
VZ
826 // if from and to are both -1, it means (in wxWindows) that all text should
827 // be selected - translate into Windows convention
828 if ( (from == -1) && (to == -1) )
829 {
830 from = 0;
831 to = -1;
832 }
833
a5aa8086
VZ
834 DoSetSelection(from, to);
835}
836
837void wxTextCtrl::DoSetSelection(long from, long to, bool scrollCaret)
838{
789295bf 839 HWND hWnd = GetHwnd();
39136494 840
2bda0e17 841#ifdef __WIN32__
aac7e7fe
VZ
842#if wxUSE_RICHEDIT
843 if ( IsRich() )
844 {
845 CHARRANGE range;
846 range.cpMin = from;
847 range.cpMax = to;
848 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
849 }
850 else
851#endif // wxUSE_RICHEDIT
852 {
853 SendMessage(hWnd, EM_SETSEL, (WPARAM)from, (LPARAM)to);
854 }
a1b82138 855
aac7e7fe 856 if ( scrollCaret )
2bda0e17 857 {
aac7e7fe 858 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
2bda0e17 859 }
aac7e7fe
VZ
860#else // Win16
861 // WPARAM is 0: selection is scrolled into view
862 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(from, to));
863#endif // Win32/16
864}
865
866// ----------------------------------------------------------------------------
867// Editing
868// ----------------------------------------------------------------------------
39136494 869
aac7e7fe
VZ
870void wxTextCtrl::Replace(long from, long to, const wxString& value)
871{
872 // Set selection and remove it
f882d57e 873 DoSetSelection(from, to, FALSE /* don't scroll caret into view */);
aac7e7fe
VZ
874
875 SendMessage(GetHwnd(), EM_REPLACESEL,
2bda0e17 876#ifdef __WIN32__
aac7e7fe 877 TRUE,
2bda0e17 878#else
aac7e7fe 879 FALSE,
2bda0e17 880#endif
aac7e7fe
VZ
881 (LPARAM)value.c_str());
882}
883
884void wxTextCtrl::Remove(long from, long to)
885{
886 Replace(from, to, _T(""));
2bda0e17
KB
887}
888
889bool wxTextCtrl::LoadFile(const wxString& file)
890{
a1b82138 891 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 892 {
a1b82138
VZ
893 // update the size limit if needed
894 AdjustSpaceLimit();
2bda0e17 895
a1b82138 896 return TRUE;
2bda0e17 897 }
2bda0e17 898
a1b82138 899 return FALSE;
2bda0e17
KB
900}
901
cd471848 902bool wxTextCtrl::IsModified() const
2bda0e17 903{
a5aa8086 904 return SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0;
2bda0e17
KB
905}
906
907// Makes 'unmodified'
cd471848 908void wxTextCtrl::DiscardEdits()
2bda0e17 909{
a1b82138 910 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
911}
912
cd471848 913int wxTextCtrl::GetNumberOfLines() const
2bda0e17 914{
789295bf 915 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
916}
917
debe6624 918long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 919{
2bda0e17 920 // This gets the char index for the _beginning_ of this line
a5aa8086
VZ
921 long charIndex = SendMessage(GetHwnd(), EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
922
923 return charIndex + x;
2bda0e17
KB
924}
925
0efe5ba7 926bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 927{
789295bf 928 HWND hWnd = GetHwnd();
2bda0e17
KB
929
930 // This gets the line number containing the character
a5aa8086 931 long lineNo;
0efe5ba7 932#if wxUSE_RICHEDIT
aac7e7fe 933 if ( IsRich() )
0efe5ba7 934 {
a5aa8086 935 lineNo = SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
0efe5ba7
VZ
936 }
937 else
938#endif // wxUSE_RICHEDIT
a5aa8086
VZ
939 {
940 lineNo = SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
941 }
0efe5ba7
VZ
942
943 if ( lineNo == -1 )
944 {
945 // no such line
946 return FALSE;
947 }
948
2bda0e17 949 // This gets the char index for the _beginning_ of this line
a5aa8086 950 long charIndex = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
951 if ( charIndex == -1 )
952 {
953 return FALSE;
954 }
955
2bda0e17 956 // The X position must therefore be the different between pos and charIndex
0efe5ba7 957 if ( x )
a5aa8086 958 *x = pos - charIndex;
0efe5ba7 959 if ( y )
a5aa8086 960 *y = lineNo;
0efe5ba7
VZ
961
962 return TRUE;
2bda0e17
KB
963}
964
debe6624 965void wxTextCtrl::ShowPosition(long pos)
2bda0e17 966{
789295bf 967 HWND hWnd = GetHwnd();
2bda0e17
KB
968
969 // To scroll to a position, we pass the number of lines and characters
970 // to scroll *by*. This means that we need to:
971 // (1) Find the line position of the current line.
972 // (2) Find the line position of pos.
973 // (3) Scroll by (pos - current).
974 // For now, ignore the horizontal scrolling.
975
976 // Is this where scrolling is relative to - the line containing the caret?
977 // Or is the first visible line??? Try first visible line.
978// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
979
980 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
981
982 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 983
2bda0e17
KB
984 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
985
2bda0e17 986 if (linesToScroll != 0)
de4d7713 987 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
988}
989
a5aa8086
VZ
990long wxTextCtrl::GetLengthOfLineContainingPos(long pos) const
991{
992 return ::SendMessage(GetHwnd(), EM_LINELENGTH, (WPARAM)pos, 0);
993}
994
debe6624 995int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17 996{
a5aa8086
VZ
997 long pos = XYToPosition(0, lineNo);
998
999 return GetLengthOfLineContainingPos(pos);
2bda0e17
KB
1000}
1001
debe6624 1002wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 1003{
a1b82138 1004 size_t len = (size_t)GetLineLength(lineNo) + 1;
488fe1fe 1005
f6bcfd97
BP
1006 // there must be at least enough place for the length WORD in the
1007 // buffer
1008 len += sizeof(WORD);
4438caf4 1009
f6bcfd97
BP
1010 wxString str;
1011 wxChar *buf = str.GetWriteBuf(len);
1012
33ac7e6f 1013 *(WORD *)buf = (WORD)len;
f6bcfd97
BP
1014 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
1015 buf[len] = 0;
4438caf4 1016
f6bcfd97 1017 str.UngetWriteBuf(len);
4438caf4
VZ
1018
1019 return str;
2bda0e17
KB
1020}
1021
d7eee191
VZ
1022void wxTextCtrl::SetMaxLength(unsigned long len)
1023{
1024 ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
1025}
1026
a1b82138 1027// ----------------------------------------------------------------------------
ca8b28f2 1028// Undo/redo
a1b82138
VZ
1029// ----------------------------------------------------------------------------
1030
ca8b28f2
JS
1031void wxTextCtrl::Undo()
1032{
1033 if (CanUndo())
1034 {
789295bf 1035 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
1036 }
1037}
1038
1039void wxTextCtrl::Redo()
1040{
1041 if (CanRedo())
1042 {
1043 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 1044 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
1045 }
1046}
1047
1048bool wxTextCtrl::CanUndo() const
1049{
a5aa8086 1050 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
ca8b28f2
JS
1051}
1052
1053bool wxTextCtrl::CanRedo() const
1054{
a5aa8086 1055 return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0;
ca8b28f2
JS
1056}
1057
a1b82138
VZ
1058// ----------------------------------------------------------------------------
1059// implemenation details
1060// ----------------------------------------------------------------------------
39136494 1061
2bda0e17
KB
1062void wxTextCtrl::Command(wxCommandEvent & event)
1063{
a1b82138
VZ
1064 SetValue(event.GetString());
1065 ProcessCommand (event);
2bda0e17
KB
1066}
1067
1068void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
1069{
a1b82138
VZ
1070 // By default, load the first file into the text window.
1071 if (event.GetNumberOfFiles() > 0)
1072 {
1073 LoadFile(event.GetFiles()[0]);
1074 }
2bda0e17
KB
1075}
1076
a37d422a
VZ
1077// ----------------------------------------------------------------------------
1078// kbd input processing
1079// ----------------------------------------------------------------------------
1080
1081bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg)
1082{
1083 MSG *msg = (MSG *)pMsg;
1084
1085 // check for our special keys here: if we don't do it and the parent frame
1086 // uses them as accelerators, they wouldn't work at all, so we disable
1087 // usual preprocessing for them
1088 if ( msg->message == WM_KEYDOWN )
1089 {
574c939e 1090 WORD vkey = (WORD) msg->wParam;
a37d422a
VZ
1091 if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
1092 {
1093 if ( vkey == VK_BACK )
1094 return FALSE;
1095 }
1096 else // no Alt
1097 {
1098 if ( wxIsCtrlDown() )
1099 {
1100 switch ( vkey )
1101 {
1102 case 'C':
1103 case 'V':
1104 case 'X':
1105 case VK_INSERT:
1106 case VK_DELETE:
1107 case VK_HOME:
1108 case VK_END:
1109 return FALSE;
1110 }
1111 }
1112 else if ( wxIsShiftDown() )
1113 {
1114 if ( vkey == VK_INSERT || vkey == VK_DELETE )
1115 return FALSE;
1116 }
1117 }
1118 }
1119
1120 return wxControl::MSWShouldPreProcessMessage(pMsg);
1121}
1122
2bda0e17
KB
1123void wxTextCtrl::OnChar(wxKeyEvent& event)
1124{
42e69d6b 1125 switch ( event.KeyCode() )
cd471848 1126 {
cd471848 1127 case WXK_RETURN:
39136494 1128 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
1129 {
1130 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
bfbd6dc1 1131 InitCommandEvent(event);
f6bcfd97 1132 event.SetString(GetValue());
cd471848
VZ
1133 if ( GetEventHandler()->ProcessEvent(event) )
1134 return;
1135 }
5fb9fcfc
VZ
1136 //else: multiline controls need Enter for themselves
1137
1138 break;
4d91c1d1 1139
cd471848 1140 case WXK_TAB:
319fefa9
VZ
1141 // always produce navigation event - even if we process TAB
1142 // ourselves the fact that we got here means that the user code
1143 // decided to skip processing of this TAB - probably to let it
1144 // do its default job.
cd471848 1145 {
5fb9fcfc
VZ
1146 wxNavigationKeyEvent eventNav;
1147 eventNav.SetDirection(!event.ShiftDown());
8614c467 1148 eventNav.SetWindowChange(event.ControlDown());
5fb9fcfc 1149 eventNav.SetEventObject(this);
39136494 1150
d9506e77 1151 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
1152 return;
1153 }
341c92a8 1154 break;
cd471848 1155 }
39136494 1156
8614c467 1157 // no, we didn't process it
42e69d6b 1158 event.Skip();
2bda0e17
KB
1159}
1160
0cf5b099
VZ
1161long wxTextCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
1162{
1163 // we always want the characters and the arrows
1164 if ( nMsg == WM_GETDLGCODE )
1165 {
1166 // we always want the chars and the arrows
1167 long lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
1168
1169 // we may have several different cases:
1170 // 1. normal case: both TAB and ENTER are used for dialog navigation
1171 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
1172 // control in the dialog
1173 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
1174 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
1175 // the next control
1176 if ( m_windowStyle & wxTE_PROCESS_ENTER )
1177 lDlgCode |= DLGC_WANTMESSAGE;
1178 if ( m_windowStyle & wxTE_PROCESS_TAB )
1179 lDlgCode |= DLGC_WANTTAB;
1180
1181 return lDlgCode;
1182 }
1183
1184 return wxTextCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
1185}
1186
debe6624 1187bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 1188{
789295bf
VZ
1189 switch (param)
1190 {
1191 case EN_SETFOCUS:
1192 case EN_KILLFOCUS:
1193 {
1194 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
d7eee191
VZ
1195 : wxEVT_SET_FOCUS,
1196 m_windowId);
789295bf
VZ
1197 event.SetEventObject( this );
1198 GetEventHandler()->ProcessEvent(event);
1199 }
1200 break;
ae29de83 1201
789295bf
VZ
1202 case EN_CHANGE:
1203 {
1204 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
bfbd6dc1
VZ
1205 InitCommandEvent(event);
1206 event.SetString(GetValue());
1207 ProcessCommand(event);
789295bf
VZ
1208 }
1209 break;
2bda0e17 1210
b12915c1 1211 case EN_MAXTEXT:
789295bf 1212 // the text size limit has been hit - increase it
d7eee191
VZ
1213 if ( !AdjustSpaceLimit() )
1214 {
1215 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId);
1216 InitCommandEvent(event);
1217 event.SetString(GetValue());
1218 ProcessCommand(event);
1219 }
789295bf
VZ
1220 break;
1221
1222 // the other notification messages are not processed
1223 case EN_UPDATE:
b12915c1 1224 case EN_ERRSPACE:
789295bf
VZ
1225 case EN_HSCROLL:
1226 case EN_VSCROLL:
f6bcfd97 1227 return FALSE;
789295bf
VZ
1228 default:
1229 return FALSE;
1230 }
1231
1232 // processed
1233 return TRUE;
2bda0e17
KB
1234}
1235
33ac7e6f 1236WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
1237#if wxUSE_CTL3D
1238 WXUINT message,
1239 WXWPARAM wParam,
1240 WXLPARAM lParam
1241#else
33ac7e6f
KB
1242 WXUINT WXUNUSED(message),
1243 WXWPARAM WXUNUSED(wParam),
788722ac
JS
1244 WXLPARAM WXUNUSED(lParam)
1245#endif
1246 )
f6bcfd97
BP
1247{
1248#if wxUSE_CTL3D
1249 if ( m_useCtl3D )
1250 {
1251 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
1252 return (WXHBRUSH) hbrush;
1253 }
1254#endif // wxUSE_CTL3D
1255
1256 HDC hdc = (HDC)pDC;
1257 if (GetParent()->GetTransparentBackground())
1258 SetBkMode(hdc, TRANSPARENT);
1259 else
1260 SetBkMode(hdc, OPAQUE);
1261
1262 wxColour colBack = GetBackgroundColour();
1263
1264 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
a756f210 1265 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
f6bcfd97
BP
1266
1267 ::SetBkColor(hdc, wxColourToRGB(colBack));
1268 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
1269
1270 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
1271
1272 return (WXHBRUSH)brush->GetResourceHandle();
1273}
1274
1275// In WIN16, need to override normal erasing because
1276// Ctl3D doesn't use the wxWindows background colour.
1277#ifdef __WIN16__
1278void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1279{
1280 wxColour col(m_backgroundColour);
1281
1282#if wxUSE_CTL3D
1283 if (m_useCtl3D)
a756f210 1284 col = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
f6bcfd97
BP
1285#endif
1286
1287 RECT rect;
1288 ::GetClientRect(GetHwnd(), &rect);
1289
a5aa8086 1290 COLORREF ref = wxColourToRGB(col);
f6bcfd97
BP
1291 HBRUSH hBrush = ::CreateSolidBrush(ref);
1292 if ( !hBrush )
1293 wxLogLastError(wxT("CreateSolidBrush"));
1294
1295 HDC hdc = (HDC)event.GetDC()->GetHDC();
1296
1297 int mode = ::SetMapMode(hdc, MM_TEXT);
1298
1299 ::FillRect(hdc, &rect, hBrush);
1300 ::DeleteObject(hBrush);
1301 ::SetMapMode(hdc, mode);
1302
1303}
aac7e7fe 1304#endif // Win16
f6bcfd97 1305
d7eee191 1306bool wxTextCtrl::AdjustSpaceLimit()
789295bf 1307{
25889d3c 1308#ifndef __WIN16__
d7eee191
VZ
1309 unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
1310
1311 // HACK: we try to automatically extend the limit for the amount of text
1312 // to allow (interactively) entering more than 64Kb of text under
1313 // Win9x but we shouldn't reset the text limit which was previously
1314 // set explicitly with SetMaxLength()
1315 //
1316 // we could solve this by storing the limit we set in wxTextCtrl but
1317 // to save space we prefer to simply test here the actual limit
1318 // value: we consider that SetMaxLength() can only be called for
1319 // values < 32Kb
1320 if ( limit < 0x8000 )
1321 {
1322 // we've got more text than limit set by SetMaxLength()
1323 return FALSE;
1324 }
1325
1326 unsigned int len = ::GetWindowTextLength(GetHwnd());
17d8ee1c 1327 if ( len >= limit )
789295bf
VZ
1328 {
1329 limit = len + 0x8000; // 32Kb
1330
5ea105e0 1331#if wxUSE_RICHEDIT
aac7e7fe 1332 if ( IsRich() )
b12915c1
VZ
1333 {
1334 // as a nice side effect, this also allows passing limit > 64Kb
1335 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
1336 }
789295bf 1337 else
b12915c1
VZ
1338#endif // wxUSE_RICHEDIT
1339 {
1340 if ( limit > 0xffff )
1341 {
1342 // this will set it to a platform-dependent maximum (much more
1343 // than 64Kb under NT)
1344 limit = 0;
1345 }
1346
789295bf 1347 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
b12915c1 1348 }
789295bf 1349 }
b12915c1 1350#endif // !Win16
d7eee191
VZ
1351
1352 // we changed the limit
1353 return TRUE;
789295bf 1354}
2bda0e17 1355
a1b82138 1356bool wxTextCtrl::AcceptsFocus() const
2bda0e17 1357{
a1b82138
VZ
1358 // we don't want focus if we can't be edited
1359 return IsEditable() && wxControl::AcceptsFocus();
1360}
c085e333 1361
f68586e5 1362wxSize wxTextCtrl::DoGetBestSize() const
a1b82138
VZ
1363{
1364 int cx, cy;
1365 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1366
1367 int wText = DEFAULT_ITEM_WIDTH;
1368
1369 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1370 if ( m_windowStyle & wxTE_MULTILINE )
1371 {
3988b155 1372 hText *= wxMax(GetNumberOfLines(), 5);
a1b82138
VZ
1373 }
1374 //else: for single line control everything is ok
1375
1376 return wxSize(wText, hText);
2bda0e17 1377}
a1b82138
VZ
1378
1379// ----------------------------------------------------------------------------
1380// standard handlers for standard edit menu events
1381// ----------------------------------------------------------------------------
2bda0e17 1382
bfbd6dc1 1383void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1384{
1385 Cut();
1386}
1387
bfbd6dc1 1388void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1389{
1390 Copy();
1391}
1392
bfbd6dc1 1393void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1394{
1395 Paste();
1396}
1397
bfbd6dc1 1398void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1399{
1400 Undo();
1401}
1402
bfbd6dc1 1403void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1404{
1405 Redo();
1406}
1407
1408void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1409{
1410 event.Enable( CanCut() );
1411}
1412
1413void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1414{
1415 event.Enable( CanCopy() );
1416}
1417
1418void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1419{
1420 event.Enable( CanPaste() );
1421}
1422
1423void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1424{
1425 event.Enable( CanUndo() );
1426}
1427
1428void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1429{
1430 event.Enable( CanRedo() );
1431}
1432
4bc1afd5
VZ
1433// the rest of the file only deals with the rich edit controls
1434#if wxUSE_RICHEDIT
1435
c57e3339
VZ
1436// ----------------------------------------------------------------------------
1437// EN_LINK processing
1438// ----------------------------------------------------------------------------
1439
574c939e 1440bool wxTextCtrl::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
c57e3339
VZ
1441{
1442 NMHDR *hdr = (NMHDR* )lParam;
1443 if ( hdr->code == EN_LINK )
1444 {
1445 ENLINK *enlink = (ENLINK *)hdr;
1446
1447 switch ( enlink->msg )
1448 {
1449 case WM_SETCURSOR:
1450 // ok, so it is hardcoded - do we really nee to customize it?
1451 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND)));
1452 *result = TRUE;
1453 break;
1454
1455 case WM_MOUSEMOVE:
1456 case WM_LBUTTONDOWN:
1457 case WM_LBUTTONUP:
1458 case WM_LBUTTONDBLCLK:
1459 case WM_RBUTTONDOWN:
1460 case WM_RBUTTONUP:
1461 case WM_RBUTTONDBLCLK:
1462 // send a mouse event
1463 {
1464 static const wxEventType eventsMouse[] =
1465 {
1466 wxEVT_MOTION,
1467 wxEVT_LEFT_DOWN,
1468 wxEVT_LEFT_UP,
1469 wxEVT_LEFT_DCLICK,
1470 wxEVT_RIGHT_DOWN,
1471 wxEVT_RIGHT_UP,
1472 wxEVT_RIGHT_DCLICK,
1473 };
1474
1475 // the event ids are consecutive
1476 wxMouseEvent
1477 evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]);
1478
1479 InitMouseEvent(evtMouse,
1480 GET_X_LPARAM(enlink->lParam),
1481 GET_Y_LPARAM(enlink->lParam),
1482 enlink->wParam);
1483
1484 wxTextUrlEvent event(m_windowId, evtMouse,
1485 enlink->chrg.cpMin,
1486 enlink->chrg.cpMax);
1487
1488 InitCommandEvent(event);
1489
1490 *result = ProcessCommand(event);
1491 }
1492 break;
1493 }
1494
1495 return TRUE;
1496 }
1497
1498 // not processed
1499 return FALSE;
1500}
1501
f6bcfd97
BP
1502// ----------------------------------------------------------------------------
1503// colour setting for the rich edit controls
1504// ----------------------------------------------------------------------------
1505
f6bcfd97
BP
1506bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
1507{
1508 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
1509 {
1510 // colour didn't really change
1511 return FALSE;
1512 }
1513
1514 if ( IsRich() )
1515 {
1516 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1517 // EM_SETBKGNDCOLOR additionally
1518 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
1519 }
1520
1521 return TRUE;
1522}
1523
1524bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1525{
1526 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
1527 {
1528 // colour didn't really change
1529 return FALSE;
1530 }
1531
1532 if ( IsRich() )
1533 {
1534 // change the colour of everything
1535 CHARFORMAT cf;
1536 wxZeroMemory(cf);
1537 cf.cbSize = sizeof(cf);
1538 cf.dwMask = CFM_COLOR;
1539 cf.crTextColor = wxColourToRGB(colour);
1540 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
1541 }
1542
1543 return TRUE;
1544}
1545
4bc1afd5
VZ
1546// ----------------------------------------------------------------------------
1547// styling support for rich edit controls
1548// ----------------------------------------------------------------------------
1549
1550bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
1551{
1552 if ( !IsRich() )
1553 {
1554 // can't do it with normal text control
1555 return FALSE;
1556 }
1557
a5aa8086
VZ
1558 // the richedit 1.0 doesn't handle setting background colour, so don't
1559 // even try to do anything if it's the only thing we want to change
1560 if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() )
4bc1afd5 1561 {
be329a3d
RD
1562 // nothing to do: return TRUE if there was really nothing to do and
1563 // FALSE if we failed to set bg colour
4bc1afd5
VZ
1564 return !style.HasBackgroundColour();
1565 }
1566
1567 // order the range if needed
1568 if ( start > end )
1569 {
1570 long tmp = start;
1571 start = end;
1572 end = tmp;
1573 }
1574
1575 // we can only change the format of the selection, so select the range we
1576 // want and restore the old selection later
1577 long startOld, endOld;
1578 GetSelection(&startOld, &endOld);
1579
1580 // but do we really have to change the selection?
1581 bool changeSel = start != startOld || end != endOld;
1582
1583 if ( changeSel )
aac7e7fe 1584 {
f882d57e 1585 DoSetSelection(start, end, FALSE /* don't scroll caret into view */);
aac7e7fe 1586 }
4bc1afd5
VZ
1587
1588 // initialize CHARFORMAT struct
be329a3d
RD
1589#if wxUSE_RICHEDIT2
1590 CHARFORMAT2 cf;
1591#else
4bc1afd5 1592 CHARFORMAT cf;
be329a3d 1593#endif
a5aa8086 1594
4bc1afd5 1595 wxZeroMemory(cf);
a5aa8086
VZ
1596
1597 // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple
1598 // CHARFORMAT in that case
1599#if wxUSE_RICHEDIT2
1600 if ( m_verRichEdit == 1 )
1601 {
1602 // this is the only thing the control is going to grok
1603 cf.cbSize = sizeof(CHARFORMAT);
1604 }
1605 else
1606#endif
1607 {
1608 // CHARFORMAT or CHARFORMAT2
1609 cf.cbSize = sizeof(cf);
1610 }
4bc1afd5
VZ
1611
1612 if ( style.HasFont() )
1613 {
aac7e7fe
VZ
1614 // VZ: CFM_CHARSET doesn't seem to do anything at all in RichEdit 2.0
1615 // but using it doesn't seem to hurt neither so leaving it for now
1616
784164e1
VZ
1617 cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET |
1618 CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE;
4bc1afd5
VZ
1619
1620 // fill in data from LOGFONT but recalculate lfHeight because we need
1621 // the real height in twips and not the negative number which
1622 // wxFillLogFont() returns (this is correct in general and works with
1623 // the Windows font mapper, but not here)
1624 LOGFONT lf;
1625 wxFillLogFont(&lf, &style.GetFont());
1626 cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
1627 cf.bCharSet = lf.lfCharSet;
1628 cf.bPitchAndFamily = lf.lfPitchAndFamily;
1629 wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
1630
784164e1
VZ
1631 // also deal with underline/italic/bold attributes: note that we must
1632 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1633 // style to allow clearing it
4bc1afd5
VZ
1634 if ( lf.lfItalic )
1635 {
4bc1afd5
VZ
1636 cf.dwEffects |= CFE_ITALIC;
1637 }
1638
1639 if ( lf.lfWeight == FW_BOLD )
1640 {
4bc1afd5
VZ
1641 cf.dwEffects |= CFE_BOLD;
1642 }
1643
1644 if ( lf.lfUnderline )
1645 {
4bc1afd5
VZ
1646 cf.dwEffects |= CFE_UNDERLINE;
1647 }
1648
1649 // strikeout fonts are not supported by wxWindows
1650 }
1651
1652 if ( style.HasTextColour() )
1653 {
1654 cf.dwMask |= CFM_COLOR;
1655 cf.crTextColor = wxColourToRGB(style.GetTextColour());
1656 }
1657
be329a3d 1658#if wxUSE_RICHEDIT2
a5aa8086 1659 if ( m_verRichEdit != 1 && style.HasBackgroundColour() )
be329a3d
RD
1660 {
1661 cf.dwMask |= CFM_BACKCOLOR;
1662 cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
1663 }
784164e1
VZ
1664#endif // wxUSE_RICHEDIT2
1665
4bc1afd5
VZ
1666 // do format the selection
1667 bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
1668 SCF_SELECTION, (LPARAM)&cf) != 0;
1669 if ( !ok )
1670 {
1671 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1672 }
1673
1674 if ( changeSel )
1675 {
1676 // restore the original selection
aac7e7fe 1677 DoSetSelection(startOld, endOld, FALSE);
4bc1afd5
VZ
1678 }
1679
1680 return ok;
1681}
f6bcfd97 1682
b12915c1
VZ
1683// ----------------------------------------------------------------------------
1684// wxRichEditModule
1685// ----------------------------------------------------------------------------
1686
b12915c1
VZ
1687bool wxRichEditModule::OnInit()
1688{
1689 // don't do anything - we will load it when needed
1690 return TRUE;
1691}
1692
1693void wxRichEditModule::OnExit()
1694{
a5aa8086 1695 for ( int i = 0; i < WXSIZEOF(ms_hRichEdit); i++ )
b12915c1 1696 {
a5aa8086
VZ
1697 if ( ms_hRichEdit[i] )
1698 {
1699 ::FreeLibrary(ms_hRichEdit[i]);
1700 }
b12915c1
VZ
1701 }
1702}
1703
1704/* static */
1705bool wxRichEditModule::Load(int version)
1706{
a5aa8086
VZ
1707 // we don't support loading richedit 3.0 as I don't know how to distinguish
1708 // it from 2.0 anyhow
1709 wxCHECK_MSG( version == 1 || version == 2, FALSE,
b12915c1
VZ
1710 _T("incorrect richedit control version requested") );
1711
a5aa8086
VZ
1712 // make it the index in the array
1713 version--;
1714
a5aa8086 1715 if ( ms_hRichEdit[version] == (HINSTANCE)-1 )
b12915c1 1716 {
a5aa8086
VZ
1717 // we had already tried to load it and failed
1718 return FALSE;
b12915c1
VZ
1719 }
1720
28978e0c
VZ
1721 if ( ms_hRichEdit[version] )
1722 {
1723 // we've already got this one
1724 return TRUE;
1725 }
1726
a5aa8086
VZ
1727 wxString dllname = version ? _T("riched20") : _T("riched32");
1728 dllname += _T(".dll");
b12915c1 1729
a5aa8086 1730 ms_hRichEdit[version] = ::LoadLibrary(dllname);
b12915c1 1731
a5aa8086 1732 if ( !ms_hRichEdit[version] )
b12915c1
VZ
1733 {
1734 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
1735
a5aa8086 1736 ms_hRichEdit[version] = (HINSTANCE)-1;
b12915c1
VZ
1737
1738 return FALSE;
1739 }
1740
1741 return TRUE;
1742}
1743
1744#endif // wxUSE_RICHEDIT
1745
1e6feb95 1746#endif // wxUSE_TEXTCTRL