]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
fix for BSD compilation
[wxWidgets.git] / src / msw / textctrl.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
bfbd6dc1 2// Name: msw/textctrl.cpp
2bda0e17
KB
3// Purpose: wxTextCtrl
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
c085e333 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
a1b82138
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
2bda0e17 16#ifdef __GNUG__
a1b82138 17 #pragma implementation "textctrl.h"
2bda0e17
KB
18#endif
19
a1b82138
VZ
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
2bda0e17
KB
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
a1b82138 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
1e6feb95
VZ
31#if wxUSE_TEXTCTRL
32
2bda0e17 33#ifndef WX_PRECOMP
a1b82138
VZ
34 #include "wx/textctrl.h"
35 #include "wx/settings.h"
36 #include "wx/brush.h"
37 #include "wx/utils.h"
381dd4bf 38 #include "wx/intl.h"
a1b82138 39 #include "wx/log.h"
381dd4bf 40 #include "wx/app.h"
2bda0e17
KB
41#endif
42
f6bcfd97
BP
43#include "wx/module.h"
44
47d67540 45#if wxUSE_CLIPBOARD
a1b82138 46 #include "wx/clipbrd.h"
2bda0e17
KB
47#endif
48
a1b82138
VZ
49#include "wx/textfile.h"
50
51#include <windowsx.h>
52
2bda0e17
KB
53#include "wx/msw/private.h"
54
a1b82138 55#include <string.h>
2bda0e17 56#include <stdlib.h>
a1b82138 57#include <sys/types.h>
fbc535ff 58
ae090fdb 59#if wxUSE_RICHEDIT && (!defined(__GNUWIN32_OLD__) || defined(__CYGWIN10__))
cd471848 60 #include <richedit.h>
2bda0e17
KB
61#endif
62
e80591b9
VZ
63// old mingw32 doesn't define this
64#ifndef CFM_CHARSET
65 #define CFM_CHARSET 0x08000000
66#endif // CFM_CHARSET
67
784164e1
VZ
68#ifndef CFM_BACKCOLOR
69 #define CFM_BACKCOLOR 0x04000000
70#endif
71
733e8cf3
VZ
72// cygwin does not have these defined for richedit
73#ifndef ENM_LINK
74 #define ENM_LINK 0x04000000
75#endif
76
77#ifndef EM_AUTOURLDETECT
78 #define EM_AUTOURLDETECT (WM_USER + 91)
79#endif
80
81#ifndef EN_LINK
82 #define EN_LINK 0x070b
83
84 typedef struct _enlink
85 {
86 NMHDR nmhdr;
87 UINT msg;
88 WPARAM wParam;
89 LPARAM lParam;
90 CHARRANGE chrg;
91 } ENLINK;
92#endif // ENLINK
93
b12915c1
VZ
94// ----------------------------------------------------------------------------
95// private classes
96// ----------------------------------------------------------------------------
97
98#if wxUSE_RICHEDIT
99
100// this module initializes RichEdit DLL if needed
101class wxRichEditModule : public wxModule
102{
103public:
104 virtual bool OnInit();
105 virtual void OnExit();
106
107 // get the version currently loaded, -1 if none
108 static int GetLoadedVersion() { return ms_verRichEdit; }
109
110 // load the richedit DLL of at least of required version
111 static bool Load(int version = 1);
112
113private:
114 // the handle to richedit DLL and the version of the DLL loaded
115 static HINSTANCE ms_hRichEdit;
116
117 // the DLL version loaded or -1 if none
118 static int ms_verRichEdit;
119
120 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
121};
122
123HINSTANCE wxRichEditModule::ms_hRichEdit = (HINSTANCE)NULL;
124int wxRichEditModule::ms_verRichEdit = -1;
125
126IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
127
128#endif // wxUSE_RICHEDIT
e702ff0f 129
a1b82138
VZ
130// ----------------------------------------------------------------------------
131// event tables and other macros
132// ----------------------------------------------------------------------------
133
2bda0e17
KB
134IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
135
136BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
a1b82138
VZ
137 EVT_CHAR(wxTextCtrl::OnChar)
138 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
139
140 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
141 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
142 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
143 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
144 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
145
146 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
147 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
148 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
149 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
150 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
f6bcfd97
BP
151#ifdef __WIN16__
152 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
153#endif
2bda0e17 154END_EVENT_TABLE()
e702ff0f 155
a1b82138
VZ
156// ============================================================================
157// implementation
158// ============================================================================
159
160// ----------------------------------------------------------------------------
161// creation
162// ----------------------------------------------------------------------------
163
cd471848 164wxTextCtrl::wxTextCtrl()
2bda0e17 165{
cd471848 166#if wxUSE_RICHEDIT
a1b82138 167 m_isRich = FALSE;
cd471848 168#endif
2bda0e17
KB
169}
170
debe6624 171bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
172 const wxString& value,
173 const wxPoint& pos,
a1b82138
VZ
174 const wxSize& size,
175 long style,
c085e333
VZ
176 const wxValidator& validator,
177 const wxString& name)
2bda0e17 178{
a1b82138 179 // base initialization
8d99be5f 180 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
a1b82138 181 return FALSE;
2bda0e17 182
a1b82138
VZ
183 if ( parent )
184 parent->AddChild(this);
2bda0e17 185
a1b82138
VZ
186 // translate wxWin style flags to MSW ones, checking for consistency while
187 // doing it
7da982c4 188 long msStyle = ES_LEFT | WS_TABSTOP;
b0766406
JS
189
190 if ( m_windowStyle & wxCLIP_SIBLINGS )
191 msStyle |= WS_CLIPSIBLINGS;
192
a1b82138
VZ
193 if ( m_windowStyle & wxTE_MULTILINE )
194 {
195 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
f6bcfd97 196 wxT("wxTE_PROCESS_ENTER style is ignored for multiline text controls (they always process it)") );
5fb9fcfc 197
b70ababc
JS
198 msStyle |= ES_MULTILINE | ES_WANTRETURN;
199 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
200 msStyle |= WS_VSCROLL;
a1b82138
VZ
201 m_windowStyle |= wxTE_PROCESS_ENTER;
202 }
5a8f04e3
VZ
203 else // !multiline
204 {
205 // there is really no reason to not have this style for single line
206 // text controls
a1b82138 207 msStyle |= ES_AUTOHSCROLL;
5a8f04e3 208 }
2bda0e17 209
5a8f04e3
VZ
210 if ( m_windowStyle & wxHSCROLL )
211 msStyle |= WS_HSCROLL | ES_AUTOHSCROLL;
2c738dd8 212
5a8f04e3 213 if ( m_windowStyle & wxTE_READONLY )
a1b82138 214 msStyle |= ES_READONLY;
2bda0e17 215
5a8f04e3 216 if ( m_windowStyle & wxTE_PASSWORD )
a1b82138 217 msStyle |= ES_PASSWORD;
2bda0e17 218
5a8f04e3
VZ
219 if ( m_windowStyle & wxTE_AUTO_SCROLL )
220 msStyle |= ES_AUTOHSCROLL;
cfdd3a98 221
5a8f04e3
VZ
222 if ( m_windowStyle & wxTE_NOHIDESEL )
223 msStyle |= ES_NOHIDESEL;
cfdd3a98 224
101f488c
VZ
225 // we always want the characters and the arrows
226 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
227
228 // we may have several different cases:
229 // 1. normal case: both TAB and ENTER are used for dialog navigation
230 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
231 // control in the dialog
232 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
233 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
234 // the next control
235 if ( m_windowStyle & wxTE_PROCESS_ENTER )
236 m_lDlgCode |= DLGC_WANTMESSAGE;
237 if ( m_windowStyle & wxTE_PROCESS_TAB )
238 m_lDlgCode |= DLGC_WANTTAB;
239
a1b82138 240 // do create the control - either an EDIT or RICHEDIT
b12915c1 241 wxString windowClass = wxT("EDIT");
cd471848 242
57c208c5 243#if wxUSE_RICHEDIT
c49245f8 244 if ( m_windowStyle & wxTE_RICH )
a1b82138 245 {
0d0512bd
VZ
246 static bool s_errorGiven = FALSE; // MT-FIXME
247
248 // only give the error msg once if the DLL can't be loaded
249 if ( !s_errorGiven )
250 {
251 // first try to load the RichEdit DLL (will do nothing if already
252 // done)
b12915c1 253 if ( !wxRichEditModule::Load() )
0d0512bd 254 {
f6bcfd97 255 wxLogError(_("Impossible to create a rich edit control, using simple text control instead. Please reinstall riched32.dll"));
0d0512bd
VZ
256
257 s_errorGiven = TRUE;
258 }
259 }
260
261 if ( s_errorGiven )
262 {
263 m_isRich = FALSE;
264 }
265 else
266 {
267 msStyle |= ES_AUTOVSCROLL;
decb3a6a
JS
268 // Experimental: this seems to help with the scroll problem. See messages from Jekabs Andrushaitis <j.andrusaitis@konts.lv>
269 // wx-dev list, entitled "[wx-dev] wxMSW-EVT_KEY_DOWN and wxMSW-wxTextCtrl" and "[wx-dev] TextCtrl (RichEdit)"
270 // Unfortunately, showing the selection in blue when the control doesn't have
271 // the focus is non-standard behaviour, and we need to find another workaround.
272 //msStyle |= ES_NOHIDESEL ;
0d0512bd 273 m_isRich = TRUE;
b12915c1
VZ
274
275 int ver = wxRichEditModule::GetLoadedVersion();
276 if ( ver == 1 )
277 {
278 windowClass = wxT("RICHEDIT");
279 }
280 else
281 {
282#ifndef RICHEDIT_CLASS
283 wxString RICHEDIT_CLASS;
284 RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), ver);
5fb2f4ba 285#if wxUSE_UNICODE
b12915c1
VZ
286 RICHEDIT_CLASS += _T('W');
287#else // ANSI
288 RICHEDIT_CLASS += _T('A');
289#endif // Unicode/ANSI
290#endif // !RICHEDIT_CLASS
291
292 windowClass = RICHEDIT_CLASS;
293 }
0d0512bd 294 }
a1b82138
VZ
295 }
296 else
297 m_isRich = FALSE;
b12915c1 298#endif // wxUSE_RICHEDIT
2bda0e17 299
7da982c4
VZ
300 if ( !MSWCreateControl(windowClass, msStyle, pos, size, value) )
301 return FALSE;
2bda0e17 302
57c208c5 303#if wxUSE_RICHEDIT
a1b82138
VZ
304 if (m_isRich)
305 {
c57e3339
VZ
306 // have to enable events manually
307 LPARAM mask = ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE;
308
309 if ( m_windowStyle & wxTE_AUTO_URL )
310 {
311 mask |= ENM_LINK;
312
313 ::SendMessage(GetHwnd(), EM_AUTOURLDETECT, TRUE, 0);
314 }
315
316 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0, mask);
a1b82138 317 }
c57e3339 318#endif // wxUSE_RICHEDIT
2bda0e17 319
a1b82138 320 return TRUE;
2bda0e17
KB
321}
322
323// Make sure the window style (etc.) reflects the HWND style (roughly)
cd471848 324void wxTextCtrl::AdoptAttributesFromHWND()
2bda0e17 325{
c085e333 326 wxWindow::AdoptAttributesFromHWND();
2bda0e17 327
789295bf 328 HWND hWnd = GetHwnd();
a1b82138 329 long style = GetWindowLong(hWnd, GWL_STYLE);
2bda0e17 330
cd471848
VZ
331 // retrieve the style to see whether this is an edit or richedit ctrl
332#if wxUSE_RICHEDIT
837e5743 333 wxChar buf[256];
2bda0e17 334
a1b82138 335 GetClassName(hWnd, buf, WXSIZEOF(buf));
2bda0e17 336
223d09f6 337 if ( wxStricmp(buf, wxT("EDIT")) == 0 )
c085e333
VZ
338 m_isRich = FALSE;
339 else
340 m_isRich = TRUE;
a1b82138 341#endif // wxUSE_RICHEDIT
c085e333
VZ
342
343 if (style & ES_MULTILINE)
344 m_windowStyle |= wxTE_MULTILINE;
345 if (style & ES_PASSWORD)
346 m_windowStyle |= wxTE_PASSWORD;
347 if (style & ES_READONLY)
348 m_windowStyle |= wxTE_READONLY;
349 if (style & ES_WANTRETURN)
350 m_windowStyle |= wxTE_PROCESS_ENTER;
2bda0e17
KB
351}
352
a1b82138
VZ
353// ----------------------------------------------------------------------------
354// set/get the controls text
355// ----------------------------------------------------------------------------
356
cd471848 357wxString wxTextCtrl::GetValue() const
2bda0e17 358{
b12915c1
VZ
359 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
360 // retrieving more than 64Kb under Win9x
361#if wxUSE_RICHEDIT
362 if ( m_isRich )
363 {
5fb2f4ba 364 wxString str;
b12915c1 365
3988b155
VZ
366 int len = GetWindowTextLength(GetHwnd());
367 if ( len )
368 {
369 // alloc one extra WORD as needed by the control
370 wxChar *p = str.GetWriteBuf(++len);
b12915c1 371
3988b155
VZ
372 TEXTRANGE textRange;
373 textRange.chrg.cpMin = 0;
374 textRange.chrg.cpMax = -1;
375 textRange.lpstrText = p;
b12915c1 376
3988b155 377 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
b12915c1 378
3988b155
VZ
379 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for
380 // the newlines which is neither Unix nor Windows style (Win95 with
381 // riched20.dll shows this behaviour) - convert it to something
382 // reasonable
383 for ( ; *p; p++ )
384 {
385 if ( *p == _T('\r') )
386 *p = _T('\n');
387 }
388
389 str.UngetWriteBuf();
390 }
391 //else: no text at all, leave the string empty
b12915c1
VZ
392
393 return str;
394 }
395#endif // wxUSE_RICHEDIT
396
397 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
398 // same one as above for consitency
399 wxString str = wxGetWindowText(GetHWND());
400
401 return wxTextFile::Translate(str, wxTextFileType_Unix);
2bda0e17
KB
402}
403
404void wxTextCtrl::SetValue(const wxString& value)
405{
b12915c1
VZ
406 // if the text is long enough, it's faster to just set it instead of first
407 // comparing it with the old one (chances are that it will be different
408 // anyhow, this comparison is there to avoid flicker for small single-line
409 // edit controls mostly)
410 if ( (value.length() > 0x400) || (value != GetValue()) )
07cf98cb 411 {
b12915c1 412 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
c4608a8a 413
5fb2f4ba 414 SetWindowText(GetHwnd(), valueDos.c_str());
a1b82138 415
f6bcfd97
BP
416 // for compatibility with the GTK and because it is more logical, we
417 // move the cursor to the end of the text after SetValue()
418
419 // GRG, Jun/2000: Changed this back after a lot of discussion
420 // in the lists. wxWindows 2.2 will have a set of flags to
421 // customize this behaviour.
422 //SetInsertionPointEnd();
423
07cf98cb
VZ
424 AdjustSpaceLimit();
425 }
2bda0e17
KB
426}
427
a1b82138 428void wxTextCtrl::WriteText(const wxString& value)
2bda0e17 429{
a1b82138 430 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
2bda0e17 431
4bc1afd5
VZ
432#if wxUSE_RICHEDIT
433 // ensure that the new text will be in the default style
434 if ( IsRich() &&
435 (m_defaultStyle.HasFont() || m_defaultStyle.HasTextColour()) )
436 {
437 long start, end;
438 GetSelection(&start, &end);
439 SetStyle(start, end, m_defaultStyle );
440 }
441#endif // wxUSE_RICHEDIT
442
a1b82138 443 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
2bda0e17 444
a1b82138 445 AdjustSpaceLimit();
2bda0e17
KB
446}
447
a1b82138
VZ
448void wxTextCtrl::AppendText(const wxString& text)
449{
450 SetInsertionPointEnd();
451 WriteText(text);
452}
453
454void wxTextCtrl::Clear()
455{
223d09f6 456 SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
457}
458
459// ----------------------------------------------------------------------------
2bda0e17 460// Clipboard operations
a1b82138
VZ
461// ----------------------------------------------------------------------------
462
cd471848 463void wxTextCtrl::Copy()
2bda0e17 464{
e702ff0f
JS
465 if (CanCopy())
466 {
789295bf 467 HWND hWnd = GetHwnd();
e702ff0f
JS
468 SendMessage(hWnd, WM_COPY, 0, 0L);
469 }
2bda0e17
KB
470}
471
cd471848 472void wxTextCtrl::Cut()
2bda0e17 473{
e702ff0f
JS
474 if (CanCut())
475 {
789295bf 476 HWND hWnd = GetHwnd();
e702ff0f
JS
477 SendMessage(hWnd, WM_CUT, 0, 0L);
478 }
2bda0e17
KB
479}
480
cd471848 481void wxTextCtrl::Paste()
2bda0e17 482{
e702ff0f
JS
483 if (CanPaste())
484 {
789295bf 485 HWND hWnd = GetHwnd();
e702ff0f
JS
486 SendMessage(hWnd, WM_PASTE, 0, 0L);
487 }
2bda0e17
KB
488}
489
a1b82138
VZ
490bool wxTextCtrl::CanCopy() const
491{
492 // Can copy if there's a selection
493 long from, to;
494 GetSelection(& from, & to);
dbf28859 495 return (from != to) ;
a1b82138
VZ
496}
497
498bool wxTextCtrl::CanCut() const
499{
500 // Can cut if there's a selection
501 long from, to;
502 GetSelection(& from, & to);
dbf28859 503 return (from != to) && (IsEditable());
a1b82138
VZ
504}
505
506bool wxTextCtrl::CanPaste() const
507{
508#if wxUSE_RICHEDIT
509 if (m_isRich)
510 {
511 int dataFormat = 0; // 0 == any format
512 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
513 }
514#endif
515 if (!IsEditable())
516 return FALSE;
517
518 // Standard edit control: check for straight text on clipboard
519 bool isTextAvailable = FALSE;
520 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
521 {
522 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
523 ::CloseClipboard();
524 }
525
526 return isTextAvailable;
527}
528
529// ----------------------------------------------------------------------------
530// Accessors
531// ----------------------------------------------------------------------------
532
debe6624 533void wxTextCtrl::SetEditable(bool editable)
2bda0e17 534{
a1b82138
VZ
535 HWND hWnd = GetHwnd();
536 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
537}
538
debe6624 539void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 540{
a1b82138 541 HWND hWnd = GetHwnd();
2bda0e17 542#ifdef __WIN32__
57c208c5 543#if wxUSE_RICHEDIT
a1b82138
VZ
544 if ( m_isRich)
545 {
546 CHARRANGE range;
547 range.cpMin = pos;
548 range.cpMax = pos;
549 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
550 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
551 }
552 else
553#endif // wxUSE_RICHEDIT
554 {
555 SendMessage(hWnd, EM_SETSEL, pos, pos);
556 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
557 }
558#else // Win16
559 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
560#endif // Win32/16
561
decb3a6a
JS
562#if wxUSE_RICHEDIT
563 if ( !m_isRich)
564#endif
565 {
566 static const wxChar *nothing = _T("");
567 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
568 }
2bda0e17
KB
569}
570
cd471848 571void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 572{
a1b82138
VZ
573 long pos = GetLastPosition();
574 SetInsertionPoint(pos);
2bda0e17
KB
575}
576
cd471848 577long wxTextCtrl::GetInsertionPoint() const
2bda0e17 578{
57c208c5 579#if wxUSE_RICHEDIT
a1b82138
VZ
580 if (m_isRich)
581 {
582 CHARRANGE range;
583 range.cpMin = 0;
584 range.cpMax = 0;
585 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
586 return range.cpMin;
587 }
2bda0e17
KB
588#endif
589
a1b82138
VZ
590 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
591 return Pos & 0xFFFF;
2bda0e17
KB
592}
593
cd471848 594long wxTextCtrl::GetLastPosition() const
2bda0e17 595{
789295bf 596 HWND hWnd = GetHwnd();
2bda0e17
KB
597
598 // Will always return a number > 0 (according to docs)
599 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
600
601 // This gets the char index for the _beginning_ of the last line
602 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 603
2bda0e17
KB
604 // Get number of characters in the last line. We'll add this to the character
605 // index for the last line, 1st position.
606 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
607
608 return (long)(charIndex + lineLength);
609}
610
a1b82138
VZ
611// If the return values from and to are the same, there is no
612// selection.
613void wxTextCtrl::GetSelection(long* from, long* to) const
614{
615#if wxUSE_RICHEDIT
616 if (m_isRich)
617 {
618 CHARRANGE charRange;
619 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
620
621 *from = charRange.cpMin;
622 *to = charRange.cpMax;
a1b82138 623 }
4bc1afd5
VZ
624 else
625#endif // rich/!rich
626 {
627 DWORD dwStart, dwEnd;
628 WPARAM wParam = (WPARAM) &dwStart; // receives starting position
629 LPARAM lParam = (LPARAM) &dwEnd; // receives ending position
a1b82138 630
4bc1afd5 631 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
a1b82138 632
4bc1afd5
VZ
633 *from = dwStart;
634 *to = dwEnd;
635 }
a1b82138
VZ
636}
637
638bool wxTextCtrl::IsEditable() const
639{
640 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
641
642 return ((style & ES_READONLY) == 0);
643}
644
645// ----------------------------------------------------------------------------
646// Editing
647// ----------------------------------------------------------------------------
648
debe6624 649void wxTextCtrl::Replace(long from, long to, const wxString& value)
2bda0e17 650{
789295bf 651 HWND hWnd = GetHwnd();
2bda0e17
KB
652 long fromChar = from;
653 long toChar = to;
39136494 654
2bda0e17
KB
655 // Set selection and remove it
656#ifdef __WIN32__
657 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
ddab9f80 658 SendMessage(hWnd, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)value.c_str());
2bda0e17
KB
659#else
660 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
ddab9f80 661 SendMessage(hWnd, EM_REPLACESEL, (WPARAM)0, (LPARAM)value.c_str());
acbd13a3 662#endif
2bda0e17
KB
663}
664
debe6624 665void wxTextCtrl::Remove(long from, long to)
2bda0e17 666{
789295bf 667 HWND hWnd = GetHwnd();
2bda0e17
KB
668 long fromChar = from;
669 long toChar = to;
39136494 670
2bda0e17
KB
671 // Cut all selected text
672#ifdef __WIN32__
673 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
ddab9f80 674 SendMessage(hWnd, EM_REPLACESEL, (WPARAM)TRUE, (LPARAM)"");
2bda0e17
KB
675#else
676 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
ddab9f80 677 SendMessage(hWnd, EM_REPLACESEL, (WPARAM)0, (LPARAM)"");
2bda0e17 678#endif
2bda0e17
KB
679}
680
debe6624 681void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 682{
789295bf 683 HWND hWnd = GetHwnd();
2bda0e17
KB
684 long fromChar = from;
685 long toChar = to;
a1b82138
VZ
686
687 // if from and to are both -1, it means (in wxWindows) that all text should
688 // be selected. Translate into Windows convention
2bda0e17
KB
689 if ((from == -1) && (to == -1))
690 {
691 fromChar = 0;
692 toChar = -1;
693 }
39136494 694
2bda0e17
KB
695#ifdef __WIN32__
696 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
697 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
698#else
699 // WPARAM is 0: selection is scrolled into view
700 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
701#endif
702}
703
704bool wxTextCtrl::LoadFile(const wxString& file)
705{
a1b82138 706 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 707 {
a1b82138
VZ
708 // update the size limit if needed
709 AdjustSpaceLimit();
2bda0e17 710
a1b82138 711 return TRUE;
2bda0e17 712 }
2bda0e17 713
a1b82138 714 return FALSE;
2bda0e17
KB
715}
716
cd471848 717bool wxTextCtrl::IsModified() const
2bda0e17 718{
789295bf 719 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
720}
721
722// Makes 'unmodified'
cd471848 723void wxTextCtrl::DiscardEdits()
2bda0e17 724{
a1b82138 725 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
726}
727
cd471848 728int wxTextCtrl::GetNumberOfLines() const
2bda0e17 729{
789295bf 730 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
731}
732
debe6624 733long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 734{
789295bf 735 HWND hWnd = GetHwnd();
2bda0e17
KB
736
737 // This gets the char index for the _beginning_ of this line
738 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
739 return (long)(x + charIndex);
740}
741
0efe5ba7 742bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 743{
789295bf 744 HWND hWnd = GetHwnd();
2bda0e17
KB
745
746 // This gets the line number containing the character
0efe5ba7
VZ
747 int lineNo;
748#if wxUSE_RICHEDIT
749 if ( m_isRich )
750 {
751 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
752 }
753 else
754#endif // wxUSE_RICHEDIT
755 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
756
757 if ( lineNo == -1 )
758 {
759 // no such line
760 return FALSE;
761 }
762
2bda0e17
KB
763 // This gets the char index for the _beginning_ of this line
764 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
765 if ( charIndex == -1 )
766 {
767 return FALSE;
768 }
769
2bda0e17 770 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
771 if ( x )
772 *x = (long)(pos - charIndex);
773 if ( y )
774 *y = (long)lineNo;
775
776 return TRUE;
2bda0e17
KB
777}
778
debe6624 779void wxTextCtrl::ShowPosition(long pos)
2bda0e17 780{
789295bf 781 HWND hWnd = GetHwnd();
2bda0e17
KB
782
783 // To scroll to a position, we pass the number of lines and characters
784 // to scroll *by*. This means that we need to:
785 // (1) Find the line position of the current line.
786 // (2) Find the line position of pos.
787 // (3) Scroll by (pos - current).
788 // For now, ignore the horizontal scrolling.
789
790 // Is this where scrolling is relative to - the line containing the caret?
791 // Or is the first visible line??? Try first visible line.
792// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
793
794 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
795
796 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 797
2bda0e17
KB
798 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
799
2bda0e17 800 if (linesToScroll != 0)
de4d7713 801 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
802}
803
debe6624 804int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
805{
806 long charIndex = XYToPosition(0, lineNo);
4438caf4 807 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
808 return len;
809}
810
debe6624 811wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 812{
a1b82138 813 size_t len = (size_t)GetLineLength(lineNo) + 1;
488fe1fe 814
f6bcfd97
BP
815 // there must be at least enough place for the length WORD in the
816 // buffer
817 len += sizeof(WORD);
4438caf4 818
f6bcfd97
BP
819 wxString str;
820 wxChar *buf = str.GetWriteBuf(len);
821
33ac7e6f 822 *(WORD *)buf = (WORD)len;
f6bcfd97
BP
823 len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
824 buf[len] = 0;
4438caf4 825
f6bcfd97 826 str.UngetWriteBuf(len);
4438caf4
VZ
827
828 return str;
2bda0e17
KB
829}
830
d7eee191
VZ
831void wxTextCtrl::SetMaxLength(unsigned long len)
832{
833 ::SendMessage(GetHwnd(), EM_LIMITTEXT, len, 0);
834}
835
a1b82138 836// ----------------------------------------------------------------------------
ca8b28f2 837// Undo/redo
a1b82138
VZ
838// ----------------------------------------------------------------------------
839
ca8b28f2
JS
840void wxTextCtrl::Undo()
841{
842 if (CanUndo())
843 {
789295bf 844 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
845 }
846}
847
848void wxTextCtrl::Redo()
849{
850 if (CanRedo())
851 {
852 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 853 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
854 }
855}
856
857bool wxTextCtrl::CanUndo() const
858{
789295bf 859 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
860}
861
862bool wxTextCtrl::CanRedo() const
863{
789295bf 864 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
865}
866
a1b82138
VZ
867// ----------------------------------------------------------------------------
868// implemenation details
869// ----------------------------------------------------------------------------
39136494 870
2bda0e17
KB
871void wxTextCtrl::Command(wxCommandEvent & event)
872{
a1b82138
VZ
873 SetValue(event.GetString());
874 ProcessCommand (event);
2bda0e17
KB
875}
876
877void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
878{
a1b82138
VZ
879 // By default, load the first file into the text window.
880 if (event.GetNumberOfFiles() > 0)
881 {
882 LoadFile(event.GetFiles()[0]);
883 }
2bda0e17
KB
884}
885
a37d422a
VZ
886// ----------------------------------------------------------------------------
887// kbd input processing
888// ----------------------------------------------------------------------------
889
890bool wxTextCtrl::MSWShouldPreProcessMessage(WXMSG* pMsg)
891{
892 MSG *msg = (MSG *)pMsg;
893
894 // check for our special keys here: if we don't do it and the parent frame
895 // uses them as accelerators, they wouldn't work at all, so we disable
896 // usual preprocessing for them
897 if ( msg->message == WM_KEYDOWN )
898 {
899 WORD vkey = msg->wParam;
900 if ( (HIWORD(msg->lParam) & KF_ALTDOWN) == KF_ALTDOWN )
901 {
902 if ( vkey == VK_BACK )
903 return FALSE;
904 }
905 else // no Alt
906 {
907 if ( wxIsCtrlDown() )
908 {
909 switch ( vkey )
910 {
911 case 'C':
912 case 'V':
913 case 'X':
914 case VK_INSERT:
915 case VK_DELETE:
916 case VK_HOME:
917 case VK_END:
918 return FALSE;
919 }
920 }
921 else if ( wxIsShiftDown() )
922 {
923 if ( vkey == VK_INSERT || vkey == VK_DELETE )
924 return FALSE;
925 }
926 }
927 }
928
929 return wxControl::MSWShouldPreProcessMessage(pMsg);
930}
931
2bda0e17
KB
932void wxTextCtrl::OnChar(wxKeyEvent& event)
933{
42e69d6b 934 switch ( event.KeyCode() )
cd471848 935 {
cd471848 936 case WXK_RETURN:
39136494 937 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
938 {
939 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
bfbd6dc1 940 InitCommandEvent(event);
f6bcfd97 941 event.SetString(GetValue());
cd471848
VZ
942 if ( GetEventHandler()->ProcessEvent(event) )
943 return;
944 }
5fb9fcfc
VZ
945 //else: multiline controls need Enter for themselves
946
947 break;
4d91c1d1 948
cd471848 949 case WXK_TAB:
319fefa9
VZ
950 // always produce navigation event - even if we process TAB
951 // ourselves the fact that we got here means that the user code
952 // decided to skip processing of this TAB - probably to let it
953 // do its default job.
cd471848 954 {
5fb9fcfc
VZ
955 wxNavigationKeyEvent eventNav;
956 eventNav.SetDirection(!event.ShiftDown());
8614c467 957 eventNav.SetWindowChange(event.ControlDown());
5fb9fcfc 958 eventNav.SetEventObject(this);
39136494 959
d9506e77 960 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
961 return;
962 }
341c92a8 963 break;
cd471848 964 }
39136494 965
8614c467 966 // no, we didn't process it
42e69d6b 967 event.Skip();
2bda0e17
KB
968}
969
debe6624 970bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 971{
789295bf
VZ
972 switch (param)
973 {
974 case EN_SETFOCUS:
975 case EN_KILLFOCUS:
976 {
977 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
d7eee191
VZ
978 : wxEVT_SET_FOCUS,
979 m_windowId);
789295bf
VZ
980 event.SetEventObject( this );
981 GetEventHandler()->ProcessEvent(event);
982 }
983 break;
ae29de83 984
789295bf
VZ
985 case EN_CHANGE:
986 {
987 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
bfbd6dc1
VZ
988 InitCommandEvent(event);
989 event.SetString(GetValue());
990 ProcessCommand(event);
789295bf
VZ
991 }
992 break;
2bda0e17 993
b12915c1 994 case EN_MAXTEXT:
789295bf 995 // the text size limit has been hit - increase it
d7eee191
VZ
996 if ( !AdjustSpaceLimit() )
997 {
998 wxCommandEvent event(wxEVT_COMMAND_TEXT_MAXLEN, m_windowId);
999 InitCommandEvent(event);
1000 event.SetString(GetValue());
1001 ProcessCommand(event);
1002 }
789295bf
VZ
1003 break;
1004
1005 // the other notification messages are not processed
1006 case EN_UPDATE:
b12915c1 1007 case EN_ERRSPACE:
789295bf
VZ
1008 case EN_HSCROLL:
1009 case EN_VSCROLL:
f6bcfd97 1010 return FALSE;
789295bf
VZ
1011 default:
1012 return FALSE;
1013 }
1014
1015 // processed
1016 return TRUE;
2bda0e17
KB
1017}
1018
33ac7e6f 1019WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
788722ac
JS
1020#if wxUSE_CTL3D
1021 WXUINT message,
1022 WXWPARAM wParam,
1023 WXLPARAM lParam
1024#else
33ac7e6f
KB
1025 WXUINT WXUNUSED(message),
1026 WXWPARAM WXUNUSED(wParam),
788722ac
JS
1027 WXLPARAM WXUNUSED(lParam)
1028#endif
1029 )
f6bcfd97
BP
1030{
1031#if wxUSE_CTL3D
1032 if ( m_useCtl3D )
1033 {
1034 HBRUSH hbrush = Ctl3dCtlColorEx(message, wParam, lParam);
1035 return (WXHBRUSH) hbrush;
1036 }
1037#endif // wxUSE_CTL3D
1038
1039 HDC hdc = (HDC)pDC;
1040 if (GetParent()->GetTransparentBackground())
1041 SetBkMode(hdc, TRANSPARENT);
1042 else
1043 SetBkMode(hdc, OPAQUE);
1044
1045 wxColour colBack = GetBackgroundColour();
1046
1047 if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0)
1048 colBack = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
1049
1050 ::SetBkColor(hdc, wxColourToRGB(colBack));
1051 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
1052
1053 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
1054
1055 return (WXHBRUSH)brush->GetResourceHandle();
1056}
1057
1058// In WIN16, need to override normal erasing because
1059// Ctl3D doesn't use the wxWindows background colour.
1060#ifdef __WIN16__
1061void wxTextCtrl::OnEraseBackground(wxEraseEvent& event)
1062{
1063 wxColour col(m_backgroundColour);
1064
1065#if wxUSE_CTL3D
1066 if (m_useCtl3D)
1067 col = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW);
1068#endif
1069
1070 RECT rect;
1071 ::GetClientRect(GetHwnd(), &rect);
1072
1073 COLORREF ref = PALETTERGB(col.Red(),
1074 col.Green(),
1075 col.Blue());
1076 HBRUSH hBrush = ::CreateSolidBrush(ref);
1077 if ( !hBrush )
1078 wxLogLastError(wxT("CreateSolidBrush"));
1079
1080 HDC hdc = (HDC)event.GetDC()->GetHDC();
1081
1082 int mode = ::SetMapMode(hdc, MM_TEXT);
1083
1084 ::FillRect(hdc, &rect, hBrush);
1085 ::DeleteObject(hBrush);
1086 ::SetMapMode(hdc, mode);
1087
1088}
1089#endif
1090
d7eee191 1091bool wxTextCtrl::AdjustSpaceLimit()
789295bf 1092{
25889d3c 1093#ifndef __WIN16__
d7eee191
VZ
1094 unsigned int limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
1095
1096 // HACK: we try to automatically extend the limit for the amount of text
1097 // to allow (interactively) entering more than 64Kb of text under
1098 // Win9x but we shouldn't reset the text limit which was previously
1099 // set explicitly with SetMaxLength()
1100 //
1101 // we could solve this by storing the limit we set in wxTextCtrl but
1102 // to save space we prefer to simply test here the actual limit
1103 // value: we consider that SetMaxLength() can only be called for
1104 // values < 32Kb
1105 if ( limit < 0x8000 )
1106 {
1107 // we've got more text than limit set by SetMaxLength()
1108 return FALSE;
1109 }
1110
1111 unsigned int len = ::GetWindowTextLength(GetHwnd());
17d8ee1c 1112 if ( len >= limit )
789295bf
VZ
1113 {
1114 limit = len + 0x8000; // 32Kb
1115
5ea105e0 1116#if wxUSE_RICHEDIT
b12915c1
VZ
1117 if ( m_isRich )
1118 {
1119 // as a nice side effect, this also allows passing limit > 64Kb
1120 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
1121 }
789295bf 1122 else
b12915c1
VZ
1123#endif // wxUSE_RICHEDIT
1124 {
1125 if ( limit > 0xffff )
1126 {
1127 // this will set it to a platform-dependent maximum (much more
1128 // than 64Kb under NT)
1129 limit = 0;
1130 }
1131
789295bf 1132 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
b12915c1 1133 }
789295bf 1134 }
b12915c1 1135#endif // !Win16
d7eee191
VZ
1136
1137 // we changed the limit
1138 return TRUE;
789295bf 1139}
2bda0e17 1140
a1b82138 1141bool wxTextCtrl::AcceptsFocus() const
2bda0e17 1142{
a1b82138
VZ
1143 // we don't want focus if we can't be edited
1144 return IsEditable() && wxControl::AcceptsFocus();
1145}
c085e333 1146
f68586e5 1147wxSize wxTextCtrl::DoGetBestSize() const
a1b82138
VZ
1148{
1149 int cx, cy;
1150 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1151
1152 int wText = DEFAULT_ITEM_WIDTH;
1153
1154 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1155 if ( m_windowStyle & wxTE_MULTILINE )
1156 {
3988b155 1157 hText *= wxMax(GetNumberOfLines(), 5);
a1b82138
VZ
1158 }
1159 //else: for single line control everything is ok
1160
1161 return wxSize(wText, hText);
2bda0e17 1162}
a1b82138
VZ
1163
1164// ----------------------------------------------------------------------------
1165// standard handlers for standard edit menu events
1166// ----------------------------------------------------------------------------
2bda0e17 1167
bfbd6dc1 1168void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1169{
1170 Cut();
1171}
1172
bfbd6dc1 1173void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1174{
1175 Copy();
1176}
1177
bfbd6dc1 1178void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1179{
1180 Paste();
1181}
1182
bfbd6dc1 1183void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1184{
1185 Undo();
1186}
1187
bfbd6dc1 1188void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1189{
1190 Redo();
1191}
1192
1193void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1194{
1195 event.Enable( CanCut() );
1196}
1197
1198void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1199{
1200 event.Enable( CanCopy() );
1201}
1202
1203void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1204{
1205 event.Enable( CanPaste() );
1206}
1207
1208void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1209{
1210 event.Enable( CanUndo() );
1211}
1212
1213void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1214{
1215 event.Enable( CanRedo() );
1216}
1217
4bc1afd5
VZ
1218// the rest of the file only deals with the rich edit controls
1219#if wxUSE_RICHEDIT
1220
c57e3339
VZ
1221// ----------------------------------------------------------------------------
1222// EN_LINK processing
1223// ----------------------------------------------------------------------------
1224
1225bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1226{
1227 NMHDR *hdr = (NMHDR* )lParam;
1228 if ( hdr->code == EN_LINK )
1229 {
1230 ENLINK *enlink = (ENLINK *)hdr;
1231
1232 switch ( enlink->msg )
1233 {
1234 case WM_SETCURSOR:
1235 // ok, so it is hardcoded - do we really nee to customize it?
1236 ::SetCursor(GetHcursorOf(wxCursor(wxCURSOR_HAND)));
1237 *result = TRUE;
1238 break;
1239
1240 case WM_MOUSEMOVE:
1241 case WM_LBUTTONDOWN:
1242 case WM_LBUTTONUP:
1243 case WM_LBUTTONDBLCLK:
1244 case WM_RBUTTONDOWN:
1245 case WM_RBUTTONUP:
1246 case WM_RBUTTONDBLCLK:
1247 // send a mouse event
1248 {
1249 static const wxEventType eventsMouse[] =
1250 {
1251 wxEVT_MOTION,
1252 wxEVT_LEFT_DOWN,
1253 wxEVT_LEFT_UP,
1254 wxEVT_LEFT_DCLICK,
1255 wxEVT_RIGHT_DOWN,
1256 wxEVT_RIGHT_UP,
1257 wxEVT_RIGHT_DCLICK,
1258 };
1259
1260 // the event ids are consecutive
1261 wxMouseEvent
1262 evtMouse(eventsMouse[enlink->msg - WM_MOUSEMOVE]);
1263
1264 InitMouseEvent(evtMouse,
1265 GET_X_LPARAM(enlink->lParam),
1266 GET_Y_LPARAM(enlink->lParam),
1267 enlink->wParam);
1268
1269 wxTextUrlEvent event(m_windowId, evtMouse,
1270 enlink->chrg.cpMin,
1271 enlink->chrg.cpMax);
1272
1273 InitCommandEvent(event);
1274
1275 *result = ProcessCommand(event);
1276 }
1277 break;
1278 }
1279
1280 return TRUE;
1281 }
1282
1283 // not processed
1284 return FALSE;
1285}
1286
f6bcfd97
BP
1287// ----------------------------------------------------------------------------
1288// colour setting for the rich edit controls
1289// ----------------------------------------------------------------------------
1290
f6bcfd97
BP
1291// Watcom C++ doesn't define this
1292#ifndef SCF_ALL
1293#define SCF_ALL 0x0004
1294#endif
1295
1296bool wxTextCtrl::SetBackgroundColour(const wxColour& colour)
1297{
1298 if ( !wxTextCtrlBase::SetBackgroundColour(colour) )
1299 {
1300 // colour didn't really change
1301 return FALSE;
1302 }
1303
1304 if ( IsRich() )
1305 {
1306 // rich edit doesn't use WM_CTLCOLOR, hence we need to send
1307 // EM_SETBKGNDCOLOR additionally
1308 ::SendMessage(GetHwnd(), EM_SETBKGNDCOLOR, 0, wxColourToRGB(colour));
1309 }
1310
1311 return TRUE;
1312}
1313
1314bool wxTextCtrl::SetForegroundColour(const wxColour& colour)
1315{
1316 if ( !wxTextCtrlBase::SetForegroundColour(colour) )
1317 {
1318 // colour didn't really change
1319 return FALSE;
1320 }
1321
1322 if ( IsRich() )
1323 {
1324 // change the colour of everything
1325 CHARFORMAT cf;
1326 wxZeroMemory(cf);
1327 cf.cbSize = sizeof(cf);
1328 cf.dwMask = CFM_COLOR;
1329 cf.crTextColor = wxColourToRGB(colour);
1330 ::SendMessage(GetHwnd(), EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf);
1331 }
1332
1333 return TRUE;
1334}
1335
4bc1afd5
VZ
1336// ----------------------------------------------------------------------------
1337// styling support for rich edit controls
1338// ----------------------------------------------------------------------------
1339
1340bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
1341{
1342 if ( !IsRich() )
1343 {
1344 // can't do it with normal text control
1345 return FALSE;
1346 }
1347
1348 // the rich text control doesn't handle setting background colour, so don't
1349 // even try if it's the only thing we want to change
be329a3d
RD
1350 if ( wxRichEditModule::GetLoadedVersion() < 2 &&
1351 !style.HasFont() && !style.HasTextColour() )
4bc1afd5 1352 {
be329a3d
RD
1353 // nothing to do: return TRUE if there was really nothing to do and
1354 // FALSE if we failed to set bg colour
4bc1afd5
VZ
1355 return !style.HasBackgroundColour();
1356 }
1357
1358 // order the range if needed
1359 if ( start > end )
1360 {
1361 long tmp = start;
1362 start = end;
1363 end = tmp;
1364 }
1365
1366 // we can only change the format of the selection, so select the range we
1367 // want and restore the old selection later
1368 long startOld, endOld;
1369 GetSelection(&startOld, &endOld);
1370
1371 // but do we really have to change the selection?
1372 bool changeSel = start != startOld || end != endOld;
1373
1374 if ( changeSel )
1375 SendMessage(GetHwnd(), EM_SETSEL, (WPARAM) start, (LPARAM) end);
1376
1377 // initialize CHARFORMAT struct
be329a3d
RD
1378#if wxUSE_RICHEDIT2
1379 CHARFORMAT2 cf;
1380#else
4bc1afd5 1381 CHARFORMAT cf;
be329a3d 1382#endif
4bc1afd5
VZ
1383 wxZeroMemory(cf);
1384 cf.cbSize = sizeof(cf);
1385
1386 if ( style.HasFont() )
1387 {
784164e1
VZ
1388 cf.dwMask |= CFM_FACE | CFM_SIZE | CFM_CHARSET |
1389 CFM_ITALIC | CFM_BOLD | CFM_UNDERLINE;
4bc1afd5
VZ
1390
1391 // fill in data from LOGFONT but recalculate lfHeight because we need
1392 // the real height in twips and not the negative number which
1393 // wxFillLogFont() returns (this is correct in general and works with
1394 // the Windows font mapper, but not here)
1395 LOGFONT lf;
1396 wxFillLogFont(&lf, &style.GetFont());
1397 cf.yHeight = 20*style.GetFont().GetPointSize(); // 1 pt = 20 twips
1398 cf.bCharSet = lf.lfCharSet;
1399 cf.bPitchAndFamily = lf.lfPitchAndFamily;
1400 wxStrncpy( cf.szFaceName, lf.lfFaceName, WXSIZEOF(cf.szFaceName) );
1401
784164e1
VZ
1402 // also deal with underline/italic/bold attributes: note that we must
1403 // always set CFM_ITALIC &c bits in dwMask, even if we don't set the
1404 // style to allow clearing it
4bc1afd5
VZ
1405 if ( lf.lfItalic )
1406 {
4bc1afd5
VZ
1407 cf.dwEffects |= CFE_ITALIC;
1408 }
1409
1410 if ( lf.lfWeight == FW_BOLD )
1411 {
4bc1afd5
VZ
1412 cf.dwEffects |= CFE_BOLD;
1413 }
1414
1415 if ( lf.lfUnderline )
1416 {
4bc1afd5
VZ
1417 cf.dwEffects |= CFE_UNDERLINE;
1418 }
1419
1420 // strikeout fonts are not supported by wxWindows
1421 }
1422
1423 if ( style.HasTextColour() )
1424 {
1425 cf.dwMask |= CFM_COLOR;
1426 cf.crTextColor = wxColourToRGB(style.GetTextColour());
1427 }
1428
be329a3d
RD
1429#if wxUSE_RICHEDIT2
1430 if ( wxRichEditModule::GetLoadedVersion() > 1 && style.HasBackgroundColour() )
1431 {
1432 cf.dwMask |= CFM_BACKCOLOR;
1433 cf.crBackColor = wxColourToRGB(style.GetBackgroundColour());
1434 }
784164e1
VZ
1435#endif // wxUSE_RICHEDIT2
1436
4bc1afd5
VZ
1437 // do format the selection
1438 bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
1439 SCF_SELECTION, (LPARAM)&cf) != 0;
1440 if ( !ok )
1441 {
1442 wxLogDebug(_T("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
1443 }
1444
1445 if ( changeSel )
1446 {
1447 // restore the original selection
1448 SendMessage(GetHwnd(), EM_SETSEL, (WPARAM)startOld, (LPARAM)endOld);
1449 }
1450
1451 return ok;
1452}
f6bcfd97 1453
b12915c1
VZ
1454// ----------------------------------------------------------------------------
1455// wxRichEditModule
1456// ----------------------------------------------------------------------------
1457
b12915c1
VZ
1458bool wxRichEditModule::OnInit()
1459{
1460 // don't do anything - we will load it when needed
1461 return TRUE;
1462}
1463
1464void wxRichEditModule::OnExit()
1465{
1466 if ( ms_hRichEdit )
1467 {
1468 FreeLibrary(ms_hRichEdit);
1469 }
1470}
1471
1472/* static */
1473bool wxRichEditModule::Load(int version)
1474{
1475 wxCHECK_MSG( version >= 1 && version <= 3, FALSE,
1476 _T("incorrect richedit control version requested") );
1477
1478 if ( version <= ms_verRichEdit )
1479 {
1480 // we've already got this or better
1481 return TRUE;
1482 }
1483
1484 if ( ms_hRichEdit )
1485 {
1486 ::FreeLibrary(ms_hRichEdit);
1487 }
1488
1489 // always try load riched20.dll first - like this we won't have to reload
1490 // it later if we're first asked for RE 1 and then for RE 2 or 3
1491 wxString dllname = _T("riched20.dll");
1492 ms_hRichEdit = ::LoadLibrary(dllname);
1493 ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2
1494
1495 if ( !ms_hRichEdit && (version == 1) )
1496 {
1497 // fall back to RE 1
1498 dllname = _T("riched32.dll");
1499 ms_hRichEdit = ::LoadLibrary(dllname);
1500 ms_verRichEdit = 1;
1501 }
1502
1503 if ( !ms_hRichEdit )
1504 {
1505 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
1506
1507 ms_verRichEdit = -1;
1508
1509 return FALSE;
1510 }
1511
1512 return TRUE;
1513}
1514
1515#endif // wxUSE_RICHEDIT
1516
1e6feb95 1517#endif // wxUSE_TEXTCTRL