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