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