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