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