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