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