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