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