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