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