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