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