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