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