]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
Clean-up, speed-up and bug-fix for wxListCtrl drawing,
[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
31#ifndef WX_PRECOMP
a1b82138
VZ
32 #include "wx/textctrl.h"
33 #include "wx/settings.h"
34 #include "wx/brush.h"
35 #include "wx/utils.h"
381dd4bf 36 #include "wx/intl.h"
a1b82138 37 #include "wx/log.h"
381dd4bf 38 #include "wx/app.h"
2bda0e17
KB
39#endif
40
47d67540 41#if wxUSE_CLIPBOARD
a1b82138 42 #include "wx/clipbrd.h"
2bda0e17
KB
43#endif
44
a1b82138
VZ
45#include "wx/textfile.h"
46
47#include <windowsx.h>
48
2bda0e17
KB
49#include "wx/msw/private.h"
50
a1b82138 51#include <string.h>
2bda0e17 52#include <stdlib.h>
a1b82138 53#include <sys/types.h>
fbc535ff
JS
54
55#if wxUSE_IOSTREAMH
3f4a0c5b 56# include <fstream.h>
fbc535ff 57#else
3f4a0c5b 58# include <fstream>
fbc535ff 59#endif
2bda0e17 60
c42404a5 61#if wxUSE_RICHEDIT && !defined(__GNUWIN32_OLD__)
cd471848 62 #include <richedit.h>
2bda0e17
KB
63#endif
64
b12915c1
VZ
65// ----------------------------------------------------------------------------
66// private classes
67// ----------------------------------------------------------------------------
68
69#if wxUSE_RICHEDIT
70
71// this module initializes RichEdit DLL if needed
72class wxRichEditModule : public wxModule
73{
74public:
75 virtual bool OnInit();
76 virtual void OnExit();
77
78 // get the version currently loaded, -1 if none
79 static int GetLoadedVersion() { return ms_verRichEdit; }
80
81 // load the richedit DLL of at least of required version
82 static bool Load(int version = 1);
83
84private:
85 // the handle to richedit DLL and the version of the DLL loaded
86 static HINSTANCE ms_hRichEdit;
87
88 // the DLL version loaded or -1 if none
89 static int ms_verRichEdit;
90
91 DECLARE_DYNAMIC_CLASS(wxRichEditModule)
92};
93
94HINSTANCE wxRichEditModule::ms_hRichEdit = (HINSTANCE)NULL;
95int wxRichEditModule::ms_verRichEdit = -1;
96
97IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule)
98
99#endif // wxUSE_RICHEDIT
e702ff0f 100
a1b82138
VZ
101// ----------------------------------------------------------------------------
102// event tables and other macros
103// ----------------------------------------------------------------------------
104
2bda0e17
KB
105IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
106
107BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
a1b82138
VZ
108 EVT_CHAR(wxTextCtrl::OnChar)
109 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
110
111 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
112 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
113 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
114 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
115 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
116
117 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
118 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
119 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
120 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
121 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
2bda0e17 122END_EVENT_TABLE()
e702ff0f 123
2bda0e17 124
a1b82138
VZ
125// ============================================================================
126// implementation
127// ============================================================================
128
129// ----------------------------------------------------------------------------
130// creation
131// ----------------------------------------------------------------------------
132
cd471848 133wxTextCtrl::wxTextCtrl()
2bda0e17 134{
cd471848 135#if wxUSE_RICHEDIT
a1b82138 136 m_isRich = FALSE;
cd471848 137#endif
2bda0e17
KB
138}
139
debe6624 140bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
141 const wxString& value,
142 const wxPoint& pos,
a1b82138
VZ
143 const wxSize& size,
144 long style,
c085e333
VZ
145 const wxValidator& validator,
146 const wxString& name)
2bda0e17 147{
a1b82138 148 // base initialization
8d99be5f 149 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
a1b82138 150 return FALSE;
2bda0e17 151
a1b82138
VZ
152 if ( parent )
153 parent->AddChild(this);
2bda0e17 154
a1b82138
VZ
155 // translate wxWin style flags to MSW ones, checking for consistency while
156 // doing it
157 long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
158 if ( m_windowStyle & wxTE_MULTILINE )
159 {
160 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
223d09f6 161 wxT("wxTE_PROCESS_ENTER style is ignored for multiline "
0d0512bd 162 "text controls (they always process it)") );
5fb9fcfc 163
b70ababc
JS
164 msStyle |= ES_MULTILINE | ES_WANTRETURN;
165 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
166 msStyle |= WS_VSCROLL;
a1b82138
VZ
167 m_windowStyle |= wxTE_PROCESS_ENTER;
168 }
169 else
170 msStyle |= ES_AUTOHSCROLL;
2bda0e17 171
2c738dd8
UM
172 if (m_windowStyle & wxHSCROLL)
173 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
174
a1b82138
VZ
175 if (m_windowStyle & wxTE_READONLY)
176 msStyle |= ES_READONLY;
2bda0e17 177
a1b82138
VZ
178 if (m_windowStyle & wxTE_PASSWORD) // hidden input
179 msStyle |= ES_PASSWORD;
2bda0e17 180
cfdd3a98
UM
181 if (m_windowStyle & wxTE_AUTO_SCROLL)
182 msStyle |= ES_AUTOHSCROLL;
183
184
101f488c
VZ
185 // we always want the characters and the arrows
186 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
187
188 // we may have several different cases:
189 // 1. normal case: both TAB and ENTER are used for dialog navigation
190 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
191 // control in the dialog
192 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
193 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
194 // the next control
195 if ( m_windowStyle & wxTE_PROCESS_ENTER )
196 m_lDlgCode |= DLGC_WANTMESSAGE;
197 if ( m_windowStyle & wxTE_PROCESS_TAB )
198 m_lDlgCode |= DLGC_WANTTAB;
199
a1b82138 200 // do create the control - either an EDIT or RICHEDIT
b12915c1 201 wxString windowClass = wxT("EDIT");
cd471848 202
57c208c5 203#if wxUSE_RICHEDIT
c49245f8 204 if ( m_windowStyle & wxTE_RICH )
a1b82138 205 {
0d0512bd
VZ
206 static bool s_errorGiven = FALSE; // MT-FIXME
207
208 // only give the error msg once if the DLL can't be loaded
209 if ( !s_errorGiven )
210 {
211 // first try to load the RichEdit DLL (will do nothing if already
212 // done)
b12915c1 213 if ( !wxRichEditModule::Load() )
0d0512bd
VZ
214 {
215 wxLogError(_("Impossible to create a rich edit control, "
b12915c1
VZ
216 "using simple text control instead. Please "
217 "reinstall riched32.dll"));
0d0512bd
VZ
218
219 s_errorGiven = TRUE;
220 }
221 }
222
223 if ( s_errorGiven )
224 {
225 m_isRich = FALSE;
226 }
227 else
228 {
229 msStyle |= ES_AUTOVSCROLL;
230 m_isRich = TRUE;
b12915c1
VZ
231
232 int ver = wxRichEditModule::GetLoadedVersion();
233 if ( ver == 1 )
234 {
235 windowClass = wxT("RICHEDIT");
236 }
237 else
238 {
239#ifndef RICHEDIT_CLASS
240 wxString RICHEDIT_CLASS;
241 RICHEDIT_CLASS.Printf(_T("RichEdit%d0"), ver);
5fb2f4ba 242#if wxUSE_UNICODE
b12915c1
VZ
243 RICHEDIT_CLASS += _T('W');
244#else // ANSI
245 RICHEDIT_CLASS += _T('A');
246#endif // Unicode/ANSI
247#endif // !RICHEDIT_CLASS
248
249 windowClass = RICHEDIT_CLASS;
250 }
0d0512bd 251 }
a1b82138
VZ
252 }
253 else
254 m_isRich = FALSE;
b12915c1 255#endif // wxUSE_RICHEDIT
2bda0e17 256
a1b82138
VZ
257 bool want3D;
258 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
2bda0e17 259
a1b82138
VZ
260 // Even with extended styles, need to combine with WS_BORDER for them to
261 // look right.
262 if ( want3D || wxStyleHasBorder(m_windowStyle) )
263 msStyle |= WS_BORDER;
2bda0e17 264
a1b82138
VZ
265 // NB: don't use pos and size as CreateWindowEx arguments because they
266 // might be -1 in which case we should use the default values (and
267 // SetSize called below takes care of it)
268 m_hWnd = (WXHWND)::CreateWindowEx(exStyle,
5fb2f4ba 269 windowClass.c_str(),
a1b82138
VZ
270 NULL,
271 msStyle,
272 0, 0, 0, 0,
273 GetHwndOf(parent),
274 (HMENU)m_windowId,
275 wxGetInstance(),
276 NULL);
2bda0e17 277
223d09f6 278 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") );
c085e333 279
1f112209 280#if wxUSE_CTL3D
a1b82138
VZ
281 if ( want3D )
282 {
283 Ctl3dSubclassCtl(GetHwnd());
284 m_useCtl3D = TRUE;
285 }
2bda0e17
KB
286#endif
287
57c208c5 288#if wxUSE_RICHEDIT
a1b82138
VZ
289 if (m_isRich)
290 {
291 // Have to enable events
292 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0,
293 ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE);
294 }
2bda0e17
KB
295#endif
296
a1b82138 297 SubclassWin(GetHWND());
2bda0e17 298
a1b82138
VZ
299 // set font, position, size and initial value
300 wxFont& fontParent = parent->GetFont();
301 if ( fontParent.Ok() )
302 {
303 SetFont(fontParent);
304 }
305 else
306 {
307 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
308 }
2bda0e17 309
a1b82138 310 // Causes a crash for Symantec C++ and WIN32 for some reason
2bda0e17 311#if !(defined(__SC__) && defined(__WIN32__))
a1b82138
VZ
312 if ( !value.IsEmpty() )
313 {
314 SetValue(value);
315 }
2bda0e17
KB
316#endif
317
d47ebd1e
JS
318 // set colours
319 SetupColours();
320
a1b82138
VZ
321 SetSize(pos.x, pos.y, size.x, size.y);
322
323 return TRUE;
2bda0e17
KB
324}
325
326// Make sure the window style (etc.) reflects the HWND style (roughly)
cd471848 327void wxTextCtrl::AdoptAttributesFromHWND()
2bda0e17 328{
c085e333 329 wxWindow::AdoptAttributesFromHWND();
2bda0e17 330
789295bf 331 HWND hWnd = GetHwnd();
a1b82138 332 long style = GetWindowLong(hWnd, GWL_STYLE);
2bda0e17 333
cd471848
VZ
334 // retrieve the style to see whether this is an edit or richedit ctrl
335#if wxUSE_RICHEDIT
837e5743 336 wxChar buf[256];
2bda0e17 337
a1b82138 338 GetClassName(hWnd, buf, WXSIZEOF(buf));
2bda0e17 339
223d09f6 340 if ( wxStricmp(buf, wxT("EDIT")) == 0 )
c085e333
VZ
341 m_isRich = FALSE;
342 else
343 m_isRich = TRUE;
a1b82138 344#endif // wxUSE_RICHEDIT
c085e333
VZ
345
346 if (style & ES_MULTILINE)
347 m_windowStyle |= wxTE_MULTILINE;
348 if (style & ES_PASSWORD)
349 m_windowStyle |= wxTE_PASSWORD;
350 if (style & ES_READONLY)
351 m_windowStyle |= wxTE_READONLY;
352 if (style & ES_WANTRETURN)
353 m_windowStyle |= wxTE_PROCESS_ENTER;
2bda0e17
KB
354}
355
cd471848 356void wxTextCtrl::SetupColours()
2bda0e17 357{
d47ebd1e 358 wxColour bkgndColour;
f6e4e9ea 359 if (IsEditable() || (m_windowStyle & wxTE_MULTILINE))
d47ebd1e
JS
360 bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW);
361 else
362 bkgndColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
363
364 SetBackgroundColour(bkgndColour);
a1b82138 365 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17
KB
366}
367
a1b82138
VZ
368// ----------------------------------------------------------------------------
369// set/get the controls text
370// ----------------------------------------------------------------------------
371
cd471848 372wxString wxTextCtrl::GetValue() const
2bda0e17 373{
b12915c1
VZ
374 // we can't use wxGetWindowText() (i.e. WM_GETTEXT internally) for
375 // retrieving more than 64Kb under Win9x
376#if wxUSE_RICHEDIT
377 if ( m_isRich )
378 {
b12915c1 379 int len = GetWindowTextLength(GetHwnd()) + 1;
5fb2f4ba
GRG
380
381 wxString str;
b12915c1
VZ
382 wxChar *p = str.GetWriteBuf(len);
383
384 TEXTRANGE textRange;
385 textRange.chrg.cpMin = 0;
386 textRange.chrg.cpMax = -1;
387 textRange.lpstrText = p;
388
389 (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange);
390
391 // believe it or not, but EM_GETTEXTRANGE uses just CR ('\r') for the
392 // newlines which is neither Unix nor Windows style (Win95 with
393 // riched20.dll shows this behaviour) - convert it to something
394 // reasonable
395 for ( ; *p; p++ )
396 {
397 if ( *p == _T('\r') )
398 *p = _T('\n');
399 }
400
401 str.UngetWriteBuf();
402
403 return str;
404 }
405#endif // wxUSE_RICHEDIT
406
407 // WM_GETTEXT uses standard DOS CR+LF (\r\n) convention - convert to the
408 // same one as above for consitency
409 wxString str = wxGetWindowText(GetHWND());
410
411 return wxTextFile::Translate(str, wxTextFileType_Unix);
2bda0e17
KB
412}
413
414void wxTextCtrl::SetValue(const wxString& value)
415{
b12915c1
VZ
416 // if the text is long enough, it's faster to just set it instead of first
417 // comparing it with the old one (chances are that it will be different
418 // anyhow, this comparison is there to avoid flicker for small single-line
419 // edit controls mostly)
420 if ( (value.length() > 0x400) || (value != GetValue()) )
07cf98cb 421 {
b12915c1 422 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
c4608a8a 423
5fb2f4ba 424 SetWindowText(GetHwnd(), valueDos.c_str());
a1b82138 425
07cf98cb
VZ
426 AdjustSpaceLimit();
427 }
2bda0e17
KB
428}
429
a1b82138 430void wxTextCtrl::WriteText(const wxString& value)
2bda0e17 431{
a1b82138 432 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
2bda0e17 433
a1b82138 434 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
2bda0e17 435
a1b82138 436 AdjustSpaceLimit();
2bda0e17
KB
437}
438
a1b82138
VZ
439void wxTextCtrl::AppendText(const wxString& text)
440{
441 SetInsertionPointEnd();
442 WriteText(text);
443}
444
445void wxTextCtrl::Clear()
446{
223d09f6 447 SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
448}
449
450// ----------------------------------------------------------------------------
2bda0e17 451// Clipboard operations
a1b82138
VZ
452// ----------------------------------------------------------------------------
453
cd471848 454void wxTextCtrl::Copy()
2bda0e17 455{
e702ff0f
JS
456 if (CanCopy())
457 {
789295bf 458 HWND hWnd = GetHwnd();
e702ff0f
JS
459 SendMessage(hWnd, WM_COPY, 0, 0L);
460 }
2bda0e17
KB
461}
462
cd471848 463void wxTextCtrl::Cut()
2bda0e17 464{
e702ff0f
JS
465 if (CanCut())
466 {
789295bf 467 HWND hWnd = GetHwnd();
e702ff0f
JS
468 SendMessage(hWnd, WM_CUT, 0, 0L);
469 }
2bda0e17
KB
470}
471
cd471848 472void wxTextCtrl::Paste()
2bda0e17 473{
e702ff0f
JS
474 if (CanPaste())
475 {
789295bf 476 HWND hWnd = GetHwnd();
e702ff0f
JS
477 SendMessage(hWnd, WM_PASTE, 0, 0L);
478 }
2bda0e17
KB
479}
480
a1b82138
VZ
481bool wxTextCtrl::CanCopy() const
482{
483 // Can copy if there's a selection
484 long from, to;
485 GetSelection(& from, & to);
486 return (from != to);
487}
488
489bool wxTextCtrl::CanCut() const
490{
491 // Can cut if there's a selection
492 long from, to;
493 GetSelection(& from, & to);
494 return (from != to);
495}
496
497bool wxTextCtrl::CanPaste() const
498{
499#if wxUSE_RICHEDIT
500 if (m_isRich)
501 {
502 int dataFormat = 0; // 0 == any format
503 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
504 }
505#endif
506 if (!IsEditable())
507 return FALSE;
508
509 // Standard edit control: check for straight text on clipboard
510 bool isTextAvailable = FALSE;
511 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
512 {
513 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
514 ::CloseClipboard();
515 }
516
517 return isTextAvailable;
518}
519
520// ----------------------------------------------------------------------------
521// Accessors
522// ----------------------------------------------------------------------------
523
debe6624 524void wxTextCtrl::SetEditable(bool editable)
2bda0e17 525{
d47ebd1e
JS
526 bool isEditable = IsEditable();
527
a1b82138
VZ
528 HWND hWnd = GetHwnd();
529 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
d47ebd1e
JS
530
531 if (editable != isEditable)
532 {
533 SetupColours();
534 Refresh();
535 }
2bda0e17
KB
536}
537
debe6624 538void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 539{
a1b82138 540 HWND hWnd = GetHwnd();
2bda0e17 541#ifdef __WIN32__
57c208c5 542#if wxUSE_RICHEDIT
a1b82138
VZ
543 if ( m_isRich)
544 {
545 CHARRANGE range;
546 range.cpMin = pos;
547 range.cpMax = pos;
548 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
549 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
550 }
551 else
552#endif // wxUSE_RICHEDIT
553 {
554 SendMessage(hWnd, EM_SETSEL, pos, pos);
555 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
556 }
557#else // Win16
558 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
559#endif // Win32/16
560
c4608a8a 561 static const wxChar *nothing = _T("");
a1b82138 562 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
2bda0e17
KB
563}
564
cd471848 565void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 566{
a1b82138
VZ
567 long pos = GetLastPosition();
568 SetInsertionPoint(pos);
2bda0e17
KB
569}
570
cd471848 571long wxTextCtrl::GetInsertionPoint() const
2bda0e17 572{
57c208c5 573#if wxUSE_RICHEDIT
a1b82138
VZ
574 if (m_isRich)
575 {
576 CHARRANGE range;
577 range.cpMin = 0;
578 range.cpMax = 0;
579 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
580 return range.cpMin;
581 }
2bda0e17
KB
582#endif
583
a1b82138
VZ
584 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
585 return Pos & 0xFFFF;
2bda0e17
KB
586}
587
cd471848 588long wxTextCtrl::GetLastPosition() const
2bda0e17 589{
789295bf 590 HWND hWnd = GetHwnd();
2bda0e17
KB
591
592 // Will always return a number > 0 (according to docs)
593 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
594
595 // This gets the char index for the _beginning_ of the last line
596 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 597
2bda0e17
KB
598 // Get number of characters in the last line. We'll add this to the character
599 // index for the last line, 1st position.
600 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
601
602 return (long)(charIndex + lineLength);
603}
604
a1b82138
VZ
605// If the return values from and to are the same, there is no
606// selection.
607void wxTextCtrl::GetSelection(long* from, long* to) const
608{
609#if wxUSE_RICHEDIT
610 if (m_isRich)
611 {
612 CHARRANGE charRange;
613 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
614
615 *from = charRange.cpMin;
616 *to = charRange.cpMax;
617
618 return;
619 }
620#endif
621 DWORD dwStart, dwEnd;
622 WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position
623 LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position
624
625 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
626
627 *from = dwStart;
628 *to = dwEnd;
629}
630
631bool wxTextCtrl::IsEditable() const
632{
633 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
634
635 return ((style & ES_READONLY) == 0);
636}
637
638// ----------------------------------------------------------------------------
639// Editing
640// ----------------------------------------------------------------------------
641
debe6624 642void wxTextCtrl::Replace(long from, long to, const wxString& value)
2bda0e17 643{
acbd13a3 644#if wxUSE_CLIPBOARD
789295bf 645 HWND hWnd = GetHwnd();
2bda0e17
KB
646 long fromChar = from;
647 long toChar = to;
39136494 648
2bda0e17
KB
649 // Set selection and remove it
650#ifdef __WIN32__
651 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
652#else
653 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
654#endif
655 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
656
657 // Now replace with 'value', by pasting.
837e5743 658 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
2bda0e17
KB
659
660 // Paste into edit control
661 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
acbd13a3
JS
662#else
663 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
664#endif
2bda0e17
KB
665}
666
debe6624 667void wxTextCtrl::Remove(long from, long to)
2bda0e17 668{
789295bf 669 HWND hWnd = GetHwnd();
2bda0e17
KB
670 long fromChar = from;
671 long toChar = to;
39136494 672
2bda0e17
KB
673 // Cut all selected text
674#ifdef __WIN32__
675 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
676#else
677 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
678#endif
679 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
680}
681
debe6624 682void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 683{
789295bf 684 HWND hWnd = GetHwnd();
2bda0e17
KB
685 long fromChar = from;
686 long toChar = to;
a1b82138
VZ
687
688 // if from and to are both -1, it means (in wxWindows) that all text should
689 // be selected. Translate into Windows convention
2bda0e17
KB
690 if ((from == -1) && (to == -1))
691 {
692 fromChar = 0;
693 toChar = -1;
694 }
39136494 695
2bda0e17
KB
696#ifdef __WIN32__
697 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
698 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
699#else
700 // WPARAM is 0: selection is scrolled into view
701 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
702#endif
703}
704
705bool wxTextCtrl::LoadFile(const wxString& file)
706{
a1b82138 707 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 708 {
a1b82138
VZ
709 // update the size limit if needed
710 AdjustSpaceLimit();
2bda0e17 711
a1b82138 712 return TRUE;
2bda0e17 713 }
2bda0e17 714
a1b82138 715 return FALSE;
2bda0e17
KB
716}
717
cd471848 718bool wxTextCtrl::IsModified() const
2bda0e17 719{
789295bf 720 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
721}
722
723// Makes 'unmodified'
cd471848 724void wxTextCtrl::DiscardEdits()
2bda0e17 725{
a1b82138 726 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
727}
728
cd471848 729int wxTextCtrl::GetNumberOfLines() const
2bda0e17 730{
789295bf 731 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
732}
733
debe6624 734long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 735{
789295bf 736 HWND hWnd = GetHwnd();
2bda0e17
KB
737
738 // This gets the char index for the _beginning_ of this line
739 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
740 return (long)(x + charIndex);
741}
742
0efe5ba7 743bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 744{
789295bf 745 HWND hWnd = GetHwnd();
2bda0e17
KB
746
747 // This gets the line number containing the character
0efe5ba7
VZ
748 int lineNo;
749#if wxUSE_RICHEDIT
750 if ( m_isRich )
751 {
752 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
753 }
754 else
755#endif // wxUSE_RICHEDIT
756 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
757
758 if ( lineNo == -1 )
759 {
760 // no such line
761 return FALSE;
762 }
763
2bda0e17
KB
764 // This gets the char index for the _beginning_ of this line
765 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
766 if ( charIndex == -1 )
767 {
768 return FALSE;
769 }
770
2bda0e17 771 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
772 if ( x )
773 *x = (long)(pos - charIndex);
774 if ( y )
775 *y = (long)lineNo;
776
777 return TRUE;
2bda0e17
KB
778}
779
debe6624 780void wxTextCtrl::ShowPosition(long pos)
2bda0e17 781{
789295bf 782 HWND hWnd = GetHwnd();
2bda0e17
KB
783
784 // To scroll to a position, we pass the number of lines and characters
785 // to scroll *by*. This means that we need to:
786 // (1) Find the line position of the current line.
787 // (2) Find the line position of pos.
788 // (3) Scroll by (pos - current).
789 // For now, ignore the horizontal scrolling.
790
791 // Is this where scrolling is relative to - the line containing the caret?
792 // Or is the first visible line??? Try first visible line.
793// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
794
795 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
796
797 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 798
2bda0e17
KB
799 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
800
2bda0e17 801 if (linesToScroll != 0)
de4d7713 802 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
803}
804
debe6624 805int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
806{
807 long charIndex = XYToPosition(0, lineNo);
4438caf4 808 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
809 return len;
810}
811
debe6624 812wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 813{
488fe1fe
VZ
814 // TODO this should probably be optimized by using GetWriteBuf()
815
a1b82138 816 size_t len = (size_t)GetLineLength(lineNo) + 1;
488fe1fe
VZ
817 if ( len < sizeof(WORD) )
818 {
819 // there must be at least enough place for the length WORD in the
820 // buffer
821 len += sizeof(WORD);
822 }
823
4438caf4
VZ
824 char *buf = (char *)malloc(len);
825 *(WORD *)buf = len;
826 int noChars = (int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
827 buf[noChars] = 0;
828
829 wxString str(buf);
830
831 free(buf);
832
833 return str;
2bda0e17
KB
834}
835
a1b82138 836// ----------------------------------------------------------------------------
ca8b28f2 837// Undo/redo
a1b82138
VZ
838// ----------------------------------------------------------------------------
839
ca8b28f2
JS
840void wxTextCtrl::Undo()
841{
842 if (CanUndo())
843 {
789295bf 844 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
845 }
846}
847
848void wxTextCtrl::Redo()
849{
850 if (CanRedo())
851 {
852 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 853 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
854 }
855}
856
857bool wxTextCtrl::CanUndo() const
858{
789295bf 859 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
860}
861
862bool wxTextCtrl::CanRedo() const
863{
789295bf 864 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
865}
866
a1b82138
VZ
867// ----------------------------------------------------------------------------
868// implemenation details
869// ----------------------------------------------------------------------------
39136494 870
2bda0e17
KB
871void wxTextCtrl::Command(wxCommandEvent & event)
872{
a1b82138
VZ
873 SetValue(event.GetString());
874 ProcessCommand (event);
2bda0e17
KB
875}
876
877void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
878{
a1b82138
VZ
879 // By default, load the first file into the text window.
880 if (event.GetNumberOfFiles() > 0)
881 {
882 LoadFile(event.GetFiles()[0]);
883 }
2bda0e17
KB
884}
885
2bda0e17
KB
886void wxTextCtrl::OnChar(wxKeyEvent& event)
887{
42e69d6b 888 switch ( event.KeyCode() )
cd471848 889 {
cd471848 890 case WXK_RETURN:
39136494 891 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
892 {
893 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
bfbd6dc1 894 InitCommandEvent(event);
cd471848
VZ
895 if ( GetEventHandler()->ProcessEvent(event) )
896 return;
897 }
5fb9fcfc
VZ
898 //else: multiline controls need Enter for themselves
899
900 break;
4d91c1d1 901
cd471848 902 case WXK_TAB:
319fefa9
VZ
903 // always produce navigation event - even if we process TAB
904 // ourselves the fact that we got here means that the user code
905 // decided to skip processing of this TAB - probably to let it
906 // do its default job.
cd471848 907 {
5fb9fcfc
VZ
908 wxNavigationKeyEvent eventNav;
909 eventNav.SetDirection(!event.ShiftDown());
8614c467 910 eventNav.SetWindowChange(event.ControlDown());
5fb9fcfc 911 eventNav.SetEventObject(this);
39136494 912
d9506e77 913 if ( GetParent()->GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
914 return;
915 }
341c92a8 916 break;
cd471848 917 }
39136494 918
8614c467 919 // no, we didn't process it
42e69d6b 920 event.Skip();
2bda0e17
KB
921}
922
debe6624 923bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 924{
789295bf
VZ
925 switch (param)
926 {
927 case EN_SETFOCUS:
928 case EN_KILLFOCUS:
929 {
930 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
931 : wxEVT_SET_FOCUS,
932 m_windowId);
933 event.SetEventObject( this );
934 GetEventHandler()->ProcessEvent(event);
935 }
936 break;
ae29de83 937
789295bf
VZ
938 case EN_CHANGE:
939 {
940 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
bfbd6dc1
VZ
941 InitCommandEvent(event);
942 event.SetString(GetValue());
943 ProcessCommand(event);
789295bf
VZ
944 }
945 break;
2bda0e17 946
b12915c1 947 case EN_MAXTEXT:
789295bf
VZ
948 // the text size limit has been hit - increase it
949 AdjustSpaceLimit();
950 break;
951
952 // the other notification messages are not processed
953 case EN_UPDATE:
b12915c1 954 case EN_ERRSPACE:
789295bf
VZ
955 case EN_HSCROLL:
956 case EN_VSCROLL:
957 default:
958 return FALSE;
959 }
960
961 // processed
962 return TRUE;
2bda0e17
KB
963}
964
789295bf
VZ
965void wxTextCtrl::AdjustSpaceLimit()
966{
25889d3c 967#ifndef __WIN16__
789295bf
VZ
968 unsigned int len = ::GetWindowTextLength(GetHwnd()),
969 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
17d8ee1c 970 if ( len >= limit )
789295bf
VZ
971 {
972 limit = len + 0x8000; // 32Kb
973
5ea105e0 974#if wxUSE_RICHEDIT
b12915c1
VZ
975 if ( m_isRich )
976 {
977 // as a nice side effect, this also allows passing limit > 64Kb
978 ::SendMessage(GetHwnd(), EM_EXLIMITTEXT, 0, limit);
979 }
789295bf 980 else
b12915c1
VZ
981#endif // wxUSE_RICHEDIT
982 {
983 if ( limit > 0xffff )
984 {
985 // this will set it to a platform-dependent maximum (much more
986 // than 64Kb under NT)
987 limit = 0;
988 }
989
789295bf 990 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
b12915c1 991 }
789295bf 992 }
b12915c1 993#endif // !Win16
789295bf 994}
2bda0e17 995
a1b82138 996bool wxTextCtrl::AcceptsFocus() const
2bda0e17 997{
a1b82138
VZ
998 // we don't want focus if we can't be edited
999 return IsEditable() && wxControl::AcceptsFocus();
1000}
c085e333 1001
f68586e5 1002wxSize wxTextCtrl::DoGetBestSize() const
a1b82138
VZ
1003{
1004 int cx, cy;
1005 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
1006
1007 int wText = DEFAULT_ITEM_WIDTH;
1008
1009 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
1010 if ( m_windowStyle & wxTE_MULTILINE )
1011 {
1012 hText *= wxMin(GetNumberOfLines(), 5);
1013 }
1014 //else: for single line control everything is ok
1015
1016 return wxSize(wText, hText);
2bda0e17 1017}
a1b82138
VZ
1018
1019// ----------------------------------------------------------------------------
1020// standard handlers for standard edit menu events
1021// ----------------------------------------------------------------------------
2bda0e17 1022
bfbd6dc1 1023void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1024{
1025 Cut();
1026}
1027
bfbd6dc1 1028void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1029{
1030 Copy();
1031}
1032
bfbd6dc1 1033void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1034{
1035 Paste();
1036}
1037
bfbd6dc1 1038void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1039{
1040 Undo();
1041}
1042
bfbd6dc1 1043void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
e702ff0f
JS
1044{
1045 Redo();
1046}
1047
1048void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
1049{
1050 event.Enable( CanCut() );
1051}
1052
1053void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
1054{
1055 event.Enable( CanCopy() );
1056}
1057
1058void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
1059{
1060 event.Enable( CanPaste() );
1061}
1062
1063void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
1064{
1065 event.Enable( CanUndo() );
1066}
1067
1068void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
1069{
1070 event.Enable( CanRedo() );
1071}
1072
b12915c1
VZ
1073// ----------------------------------------------------------------------------
1074// wxRichEditModule
1075// ----------------------------------------------------------------------------
1076
1077#if wxUSE_RICHEDIT
1078
1079bool wxRichEditModule::OnInit()
1080{
1081 // don't do anything - we will load it when needed
1082 return TRUE;
1083}
1084
1085void wxRichEditModule::OnExit()
1086{
1087 if ( ms_hRichEdit )
1088 {
1089 FreeLibrary(ms_hRichEdit);
1090 }
1091}
1092
1093/* static */
1094bool wxRichEditModule::Load(int version)
1095{
1096 wxCHECK_MSG( version >= 1 && version <= 3, FALSE,
1097 _T("incorrect richedit control version requested") );
1098
1099 if ( version <= ms_verRichEdit )
1100 {
1101 // we've already got this or better
1102 return TRUE;
1103 }
1104
1105 if ( ms_hRichEdit )
1106 {
1107 ::FreeLibrary(ms_hRichEdit);
1108 }
1109
1110 // always try load riched20.dll first - like this we won't have to reload
1111 // it later if we're first asked for RE 1 and then for RE 2 or 3
1112 wxString dllname = _T("riched20.dll");
1113 ms_hRichEdit = ::LoadLibrary(dllname);
1114 ms_verRichEdit = 2; // no way to tell if it's 2 or 3, assume 2
1115
1116 if ( !ms_hRichEdit && (version == 1) )
1117 {
1118 // fall back to RE 1
1119 dllname = _T("riched32.dll");
1120 ms_hRichEdit = ::LoadLibrary(dllname);
1121 ms_verRichEdit = 1;
1122 }
1123
1124 if ( !ms_hRichEdit )
1125 {
1126 wxLogSysError(_("Could not load Rich Edit DLL '%s'"), dllname.c_str());
1127
1128 ms_verRichEdit = -1;
1129
1130 return FALSE;
1131 }
1132
1133 return TRUE;
1134}
1135
1136#endif // wxUSE_RICHEDIT
1137