]> git.saurik.com Git - wxWidgets.git/blame - src/msw/textctrl.cpp
wxUSE_RICHeDIT added
[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
0d0512bd 61#if wxUSE_RICHEDIT
cd471848 62 #include <richedit.h>
2bda0e17
KB
63#endif
64
65#if !USE_SHARED_LIBRARY
e702ff0f 66
a1b82138
VZ
67// ----------------------------------------------------------------------------
68// event tables and other macros
69// ----------------------------------------------------------------------------
70
2bda0e17
KB
71IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
72
73BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
a1b82138
VZ
74 EVT_CHAR(wxTextCtrl::OnChar)
75 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
76
77 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
78 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
79 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
80 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
81 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
82
83 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
84 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
85 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
86 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
87 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
2bda0e17 88END_EVENT_TABLE()
e702ff0f 89
cd471848 90#endif // USE_SHARED_LIBRARY
2bda0e17 91
a1b82138
VZ
92// ============================================================================
93// implementation
94// ============================================================================
95
96// ----------------------------------------------------------------------------
97// creation
98// ----------------------------------------------------------------------------
99
cd471848 100wxTextCtrl::wxTextCtrl()
2bda0e17 101{
cd471848 102#if wxUSE_RICHEDIT
a1b82138 103 m_isRich = FALSE;
cd471848 104#endif
2bda0e17
KB
105}
106
debe6624 107bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
c085e333
VZ
108 const wxString& value,
109 const wxPoint& pos,
a1b82138
VZ
110 const wxSize& size,
111 long style,
c085e333
VZ
112 const wxValidator& validator,
113 const wxString& name)
2bda0e17 114{
a1b82138 115 // base initialization
8d99be5f 116 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
a1b82138 117 return FALSE;
2bda0e17 118
a1b82138
VZ
119 if ( parent )
120 parent->AddChild(this);
2bda0e17 121
a1b82138
VZ
122 // set colours
123 SetupColours();
2bda0e17 124
a1b82138
VZ
125 // translate wxWin style flags to MSW ones, checking for consistency while
126 // doing it
127 long msStyle = ES_LEFT | WS_VISIBLE | WS_CHILD | WS_TABSTOP;
128 if ( m_windowStyle & wxTE_MULTILINE )
129 {
130 wxASSERT_MSG( !(m_windowStyle & wxTE_PROCESS_ENTER),
223d09f6 131 wxT("wxTE_PROCESS_ENTER style is ignored for multiline "
0d0512bd 132 "text controls (they always process it)") );
5fb9fcfc 133
b70ababc
JS
134 msStyle |= ES_MULTILINE | ES_WANTRETURN;
135 if ((m_windowStyle & wxTE_NO_VSCROLL) == 0)
136 msStyle |= WS_VSCROLL;
a1b82138
VZ
137 m_windowStyle |= wxTE_PROCESS_ENTER;
138 }
139 else
140 msStyle |= ES_AUTOHSCROLL;
2bda0e17 141
2c738dd8
UM
142 if (m_windowStyle & wxHSCROLL)
143 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
144
a1b82138
VZ
145 if (m_windowStyle & wxTE_READONLY)
146 msStyle |= ES_READONLY;
2bda0e17 147
a1b82138
VZ
148 if (m_windowStyle & wxHSCROLL)
149 msStyle |= (WS_HSCROLL | ES_AUTOHSCROLL);
150 if (m_windowStyle & wxTE_PASSWORD) // hidden input
151 msStyle |= ES_PASSWORD;
2bda0e17 152
101f488c
VZ
153 // we always want the characters and the arrows
154 m_lDlgCode = DLGC_WANTCHARS | DLGC_WANTARROWS;
155
156 // we may have several different cases:
157 // 1. normal case: both TAB and ENTER are used for dialog navigation
158 // 2. ctrl which wants TAB for itself: ENTER is used to pass to the next
159 // control in the dialog
160 // 3. ctrl which wants ENTER for itself: TAB is used for dialog navigation
161 // 4. ctrl which wants both TAB and ENTER: Ctrl-ENTER is used to pass to
162 // the next control
163 if ( m_windowStyle & wxTE_PROCESS_ENTER )
164 m_lDlgCode |= DLGC_WANTMESSAGE;
165 if ( m_windowStyle & wxTE_PROCESS_TAB )
166 m_lDlgCode |= DLGC_WANTTAB;
167
a1b82138 168 // do create the control - either an EDIT or RICHEDIT
223d09f6 169 const wxChar *windowClass = wxT("EDIT");
cd471848 170
57c208c5 171#if wxUSE_RICHEDIT
c49245f8 172 if ( m_windowStyle & wxTE_RICH )
a1b82138 173 {
0d0512bd
VZ
174 static bool s_errorGiven = FALSE; // MT-FIXME
175
176 // only give the error msg once if the DLL can't be loaded
177 if ( !s_errorGiven )
178 {
179 // first try to load the RichEdit DLL (will do nothing if already
180 // done)
181 if ( !wxTheApp->InitRichEdit() )
182 {
183 wxLogError(_("Impossible to create a rich edit control, "
184 "using simple text control instead."));
185
186 s_errorGiven = TRUE;
187 }
188 }
189
190 if ( s_errorGiven )
191 {
192 m_isRich = FALSE;
193 }
194 else
195 {
196 msStyle |= ES_AUTOVSCROLL;
197 m_isRich = TRUE;
198 windowClass = wxT("RICHEDIT");
199 }
a1b82138
VZ
200 }
201 else
202 m_isRich = FALSE;
cd471848 203#endif
2bda0e17 204
a1b82138
VZ
205 bool want3D;
206 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
2bda0e17 207
a1b82138
VZ
208 // Even with extended styles, need to combine with WS_BORDER for them to
209 // look right.
210 if ( want3D || wxStyleHasBorder(m_windowStyle) )
211 msStyle |= WS_BORDER;
2bda0e17 212
a1b82138
VZ
213 // NB: don't use pos and size as CreateWindowEx arguments because they
214 // might be -1 in which case we should use the default values (and
215 // SetSize called below takes care of it)
216 m_hWnd = (WXHWND)::CreateWindowEx(exStyle,
217 windowClass,
218 NULL,
219 msStyle,
220 0, 0, 0, 0,
221 GetHwndOf(parent),
222 (HMENU)m_windowId,
223 wxGetInstance(),
224 NULL);
2bda0e17 225
223d09f6 226 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create text ctrl") );
c085e333 227
1f112209 228#if wxUSE_CTL3D
a1b82138
VZ
229 if ( want3D )
230 {
231 Ctl3dSubclassCtl(GetHwnd());
232 m_useCtl3D = TRUE;
233 }
2bda0e17
KB
234#endif
235
57c208c5 236#if wxUSE_RICHEDIT
a1b82138
VZ
237 if (m_isRich)
238 {
239 // Have to enable events
240 ::SendMessage(GetHwnd(), EM_SETEVENTMASK, 0,
241 ENM_CHANGE | ENM_DROPFILES | ENM_SELCHANGE | ENM_UPDATE);
242 }
2bda0e17
KB
243#endif
244
a1b82138 245 SubclassWin(GetHWND());
2bda0e17 246
a1b82138
VZ
247 // set font, position, size and initial value
248 wxFont& fontParent = parent->GetFont();
249 if ( fontParent.Ok() )
250 {
251 SetFont(fontParent);
252 }
253 else
254 {
255 SetFont(wxSystemSettings::GetSystemFont(wxSYS_SYSTEM_FONT));
256 }
2bda0e17 257
a1b82138 258 // Causes a crash for Symantec C++ and WIN32 for some reason
2bda0e17 259#if !(defined(__SC__) && defined(__WIN32__))
a1b82138
VZ
260 if ( !value.IsEmpty() )
261 {
262 SetValue(value);
263 }
2bda0e17
KB
264#endif
265
a1b82138
VZ
266 SetSize(pos.x, pos.y, size.x, size.y);
267
268 return TRUE;
2bda0e17
KB
269}
270
271// Make sure the window style (etc.) reflects the HWND style (roughly)
cd471848 272void wxTextCtrl::AdoptAttributesFromHWND()
2bda0e17 273{
c085e333 274 wxWindow::AdoptAttributesFromHWND();
2bda0e17 275
789295bf 276 HWND hWnd = GetHwnd();
a1b82138 277 long style = GetWindowLong(hWnd, GWL_STYLE);
2bda0e17 278
cd471848
VZ
279 // retrieve the style to see whether this is an edit or richedit ctrl
280#if wxUSE_RICHEDIT
837e5743 281 wxChar buf[256];
2bda0e17 282
a1b82138 283 GetClassName(hWnd, buf, WXSIZEOF(buf));
2bda0e17 284
223d09f6 285 if ( wxStricmp(buf, wxT("EDIT")) == 0 )
c085e333
VZ
286 m_isRich = FALSE;
287 else
288 m_isRich = TRUE;
a1b82138 289#endif // wxUSE_RICHEDIT
c085e333
VZ
290
291 if (style & ES_MULTILINE)
292 m_windowStyle |= wxTE_MULTILINE;
293 if (style & ES_PASSWORD)
294 m_windowStyle |= wxTE_PASSWORD;
295 if (style & ES_READONLY)
296 m_windowStyle |= wxTE_READONLY;
297 if (style & ES_WANTRETURN)
298 m_windowStyle |= wxTE_PROCESS_ENTER;
2bda0e17
KB
299}
300
cd471848 301void wxTextCtrl::SetupColours()
2bda0e17 302{
a1b82138
VZ
303 // FIXME why is bg colour not inherited from parent?
304 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
305 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17
KB
306}
307
a1b82138
VZ
308// ----------------------------------------------------------------------------
309// set/get the controls text
310// ----------------------------------------------------------------------------
311
cd471848 312wxString wxTextCtrl::GetValue() const
2bda0e17 313{
789295bf 314 return wxGetWindowText(GetHWND());
2bda0e17
KB
315}
316
317void wxTextCtrl::SetValue(const wxString& value)
318{
a1b82138 319 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
789295bf 320
07cf98cb
VZ
321 if ( valueDos != GetValue() )
322 {
323 SetWindowText(GetHwnd(), valueDos);
a1b82138 324
07cf98cb
VZ
325 AdjustSpaceLimit();
326 }
2bda0e17
KB
327}
328
a1b82138 329void wxTextCtrl::WriteText(const wxString& value)
2bda0e17 330{
a1b82138 331 wxString valueDos = wxTextFile::Translate(value, wxTextFileType_Dos);
2bda0e17 332
a1b82138 333 SendMessage(GetHwnd(), EM_REPLACESEL, 0, (LPARAM)valueDos.c_str());
2bda0e17 334
a1b82138 335 AdjustSpaceLimit();
2bda0e17
KB
336}
337
a1b82138
VZ
338void wxTextCtrl::AppendText(const wxString& text)
339{
340 SetInsertionPointEnd();
341 WriteText(text);
342}
343
344void wxTextCtrl::Clear()
345{
223d09f6 346 SetWindowText(GetHwnd(), wxT(""));
a1b82138
VZ
347}
348
349// ----------------------------------------------------------------------------
2bda0e17 350// Clipboard operations
a1b82138
VZ
351// ----------------------------------------------------------------------------
352
cd471848 353void wxTextCtrl::Copy()
2bda0e17 354{
e702ff0f
JS
355 if (CanCopy())
356 {
789295bf 357 HWND hWnd = GetHwnd();
e702ff0f
JS
358 SendMessage(hWnd, WM_COPY, 0, 0L);
359 }
2bda0e17
KB
360}
361
cd471848 362void wxTextCtrl::Cut()
2bda0e17 363{
e702ff0f
JS
364 if (CanCut())
365 {
789295bf 366 HWND hWnd = GetHwnd();
e702ff0f
JS
367 SendMessage(hWnd, WM_CUT, 0, 0L);
368 }
2bda0e17
KB
369}
370
cd471848 371void wxTextCtrl::Paste()
2bda0e17 372{
e702ff0f
JS
373 if (CanPaste())
374 {
789295bf 375 HWND hWnd = GetHwnd();
e702ff0f
JS
376 SendMessage(hWnd, WM_PASTE, 0, 0L);
377 }
2bda0e17
KB
378}
379
a1b82138
VZ
380bool wxTextCtrl::CanCopy() const
381{
382 // Can copy if there's a selection
383 long from, to;
384 GetSelection(& from, & to);
385 return (from != to);
386}
387
388bool wxTextCtrl::CanCut() const
389{
390 // Can cut if there's a selection
391 long from, to;
392 GetSelection(& from, & to);
393 return (from != to);
394}
395
396bool wxTextCtrl::CanPaste() const
397{
398#if wxUSE_RICHEDIT
399 if (m_isRich)
400 {
401 int dataFormat = 0; // 0 == any format
402 return (::SendMessage( GetHwnd(), EM_CANPASTE, (WPARAM) (UINT) dataFormat, 0) != 0);
403 }
404#endif
405 if (!IsEditable())
406 return FALSE;
407
408 // Standard edit control: check for straight text on clipboard
409 bool isTextAvailable = FALSE;
410 if ( ::OpenClipboard(GetHwndOf(wxTheApp->GetTopWindow())) )
411 {
412 isTextAvailable = (::IsClipboardFormatAvailable(CF_TEXT) != 0);
413 ::CloseClipboard();
414 }
415
416 return isTextAvailable;
417}
418
419// ----------------------------------------------------------------------------
420// Accessors
421// ----------------------------------------------------------------------------
422
debe6624 423void wxTextCtrl::SetEditable(bool editable)
2bda0e17 424{
a1b82138
VZ
425 HWND hWnd = GetHwnd();
426 SendMessage(hWnd, EM_SETREADONLY, (WPARAM)!editable, (LPARAM)0L);
2bda0e17
KB
427}
428
debe6624 429void wxTextCtrl::SetInsertionPoint(long pos)
2bda0e17 430{
a1b82138 431 HWND hWnd = GetHwnd();
2bda0e17 432#ifdef __WIN32__
57c208c5 433#if wxUSE_RICHEDIT
a1b82138
VZ
434 if ( m_isRich)
435 {
436 CHARRANGE range;
437 range.cpMin = pos;
438 range.cpMax = pos;
439 SendMessage(hWnd, EM_EXSETSEL, 0, (LPARAM) &range);
440 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
441 }
442 else
443#endif // wxUSE_RICHEDIT
444 {
445 SendMessage(hWnd, EM_SETSEL, pos, pos);
446 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
447 }
448#else // Win16
449 SendMessage(hWnd, EM_SETSEL, 0, MAKELPARAM(pos, pos));
450#endif // Win32/16
451
452 static const char *nothing = "";
453 SendMessage(hWnd, EM_REPLACESEL, 0, (LPARAM)nothing);
2bda0e17
KB
454}
455
cd471848 456void wxTextCtrl::SetInsertionPointEnd()
2bda0e17 457{
a1b82138
VZ
458 long pos = GetLastPosition();
459 SetInsertionPoint(pos);
2bda0e17
KB
460}
461
cd471848 462long wxTextCtrl::GetInsertionPoint() const
2bda0e17 463{
57c208c5 464#if wxUSE_RICHEDIT
a1b82138
VZ
465 if (m_isRich)
466 {
467 CHARRANGE range;
468 range.cpMin = 0;
469 range.cpMax = 0;
470 SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) &range);
471 return range.cpMin;
472 }
2bda0e17
KB
473#endif
474
a1b82138
VZ
475 DWORD Pos = (DWORD)SendMessage(GetHwnd(), EM_GETSEL, 0, 0L);
476 return Pos & 0xFFFF;
2bda0e17
KB
477}
478
cd471848 479long wxTextCtrl::GetLastPosition() const
2bda0e17 480{
789295bf 481 HWND hWnd = GetHwnd();
2bda0e17
KB
482
483 // Will always return a number > 0 (according to docs)
484 int noLines = (int)SendMessage(hWnd, EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0L);
485
486 // This gets the char index for the _beginning_ of the last line
487 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)(noLines-1), (LPARAM)0L);
39136494 488
2bda0e17
KB
489 // Get number of characters in the last line. We'll add this to the character
490 // index for the last line, 1st position.
491 int lineLength = (int)SendMessage(hWnd, EM_LINELENGTH, (WPARAM)charIndex, (LPARAM)0L);
492
493 return (long)(charIndex + lineLength);
494}
495
a1b82138
VZ
496// If the return values from and to are the same, there is no
497// selection.
498void wxTextCtrl::GetSelection(long* from, long* to) const
499{
500#if wxUSE_RICHEDIT
501 if (m_isRich)
502 {
503 CHARRANGE charRange;
504 ::SendMessage(GetHwnd(), EM_EXGETSEL, 0, (LPARAM) (CHARRANGE*) & charRange);
505
506 *from = charRange.cpMin;
507 *to = charRange.cpMax;
508
509 return;
510 }
511#endif
512 DWORD dwStart, dwEnd;
513 WPARAM wParam = (WPARAM) (DWORD*) & dwStart; // receives starting position
514 LPARAM lParam = (LPARAM) (DWORD*) & dwEnd; // receives ending position
515
516 ::SendMessage(GetHwnd(), EM_GETSEL, wParam, lParam);
517
518 *from = dwStart;
519 *to = dwEnd;
520}
521
522bool wxTextCtrl::IsEditable() const
523{
524 long style = ::GetWindowLong(GetHwnd(), GWL_STYLE);
525
526 return ((style & ES_READONLY) == 0);
527}
528
529// ----------------------------------------------------------------------------
530// Editing
531// ----------------------------------------------------------------------------
532
debe6624 533void wxTextCtrl::Replace(long from, long to, const wxString& value)
2bda0e17 534{
acbd13a3 535#if wxUSE_CLIPBOARD
789295bf 536 HWND hWnd = GetHwnd();
2bda0e17
KB
537 long fromChar = from;
538 long toChar = to;
39136494 539
2bda0e17
KB
540 // Set selection and remove it
541#ifdef __WIN32__
542 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
543#else
544 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
545#endif
546 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
547
548 // Now replace with 'value', by pasting.
837e5743 549 wxSetClipboardData(wxDF_TEXT, (wxObject *) (const wxChar *)value, 0, 0);
2bda0e17
KB
550
551 // Paste into edit control
552 SendMessage(hWnd, WM_PASTE, (WPARAM)0, (LPARAM)0L);
acbd13a3
JS
553#else
554 wxFAIL_MSG("wxTextCtrl::Replace not implemented if wxUSE_CLIPBOARD is 0.");
555#endif
2bda0e17
KB
556}
557
debe6624 558void wxTextCtrl::Remove(long from, long to)
2bda0e17 559{
789295bf 560 HWND hWnd = GetHwnd();
2bda0e17
KB
561 long fromChar = from;
562 long toChar = to;
39136494 563
2bda0e17
KB
564 // Cut all selected text
565#ifdef __WIN32__
566 SendMessage(hWnd, EM_SETSEL, fromChar, toChar);
567#else
568 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
569#endif
570 SendMessage(hWnd, WM_CUT, (WPARAM)0, (LPARAM)0);
571}
572
debe6624 573void wxTextCtrl::SetSelection(long from, long to)
2bda0e17 574{
789295bf 575 HWND hWnd = GetHwnd();
2bda0e17
KB
576 long fromChar = from;
577 long toChar = to;
a1b82138
VZ
578
579 // if from and to are both -1, it means (in wxWindows) that all text should
580 // be selected. Translate into Windows convention
2bda0e17
KB
581 if ((from == -1) && (to == -1))
582 {
583 fromChar = 0;
584 toChar = -1;
585 }
39136494 586
2bda0e17
KB
587#ifdef __WIN32__
588 SendMessage(hWnd, EM_SETSEL, (WPARAM)fromChar, (LPARAM)toChar);
589 SendMessage(hWnd, EM_SCROLLCARET, (WPARAM)0, (LPARAM)0);
590#else
591 // WPARAM is 0: selection is scrolled into view
592 SendMessage(hWnd, EM_SETSEL, (WPARAM)0, (LPARAM)MAKELONG(fromChar, toChar));
593#endif
594}
595
596bool wxTextCtrl::LoadFile(const wxString& file)
597{
a1b82138 598 if ( wxTextCtrlBase::LoadFile(file) )
cd471848 599 {
a1b82138
VZ
600 // update the size limit if needed
601 AdjustSpaceLimit();
2bda0e17 602
a1b82138 603 return TRUE;
2bda0e17 604 }
2bda0e17 605
a1b82138 606 return FALSE;
2bda0e17
KB
607}
608
cd471848 609bool wxTextCtrl::IsModified() const
2bda0e17 610{
789295bf 611 return (SendMessage(GetHwnd(), EM_GETMODIFY, 0, 0) != 0);
2bda0e17
KB
612}
613
614// Makes 'unmodified'
cd471848 615void wxTextCtrl::DiscardEdits()
2bda0e17 616{
a1b82138 617 SendMessage(GetHwnd(), EM_SETMODIFY, FALSE, 0L);
2bda0e17
KB
618}
619
cd471848 620int wxTextCtrl::GetNumberOfLines() const
2bda0e17 621{
789295bf 622 return (int)SendMessage(GetHwnd(), EM_GETLINECOUNT, (WPARAM)0, (LPARAM)0);
2bda0e17
KB
623}
624
debe6624 625long wxTextCtrl::XYToPosition(long x, long y) const
2bda0e17 626{
789295bf 627 HWND hWnd = GetHwnd();
2bda0e17
KB
628
629 // This gets the char index for the _beginning_ of this line
630 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)y, (LPARAM)0);
631 return (long)(x + charIndex);
632}
633
0efe5ba7 634bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
2bda0e17 635{
789295bf 636 HWND hWnd = GetHwnd();
2bda0e17
KB
637
638 // This gets the line number containing the character
0efe5ba7
VZ
639 int lineNo;
640#if wxUSE_RICHEDIT
641 if ( m_isRich )
642 {
643 lineNo = (int)SendMessage(hWnd, EM_EXLINEFROMCHAR, 0, (LPARAM)pos);
644 }
645 else
646#endif // wxUSE_RICHEDIT
647 lineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, 0);
648
649 if ( lineNo == -1 )
650 {
651 // no such line
652 return FALSE;
653 }
654
2bda0e17
KB
655 // This gets the char index for the _beginning_ of this line
656 int charIndex = (int)SendMessage(hWnd, EM_LINEINDEX, (WPARAM)lineNo, (LPARAM)0);
0efe5ba7
VZ
657 if ( charIndex == -1 )
658 {
659 return FALSE;
660 }
661
2bda0e17 662 // The X position must therefore be the different between pos and charIndex
0efe5ba7
VZ
663 if ( x )
664 *x = (long)(pos - charIndex);
665 if ( y )
666 *y = (long)lineNo;
667
668 return TRUE;
2bda0e17
KB
669}
670
debe6624 671void wxTextCtrl::ShowPosition(long pos)
2bda0e17 672{
789295bf 673 HWND hWnd = GetHwnd();
2bda0e17
KB
674
675 // To scroll to a position, we pass the number of lines and characters
676 // to scroll *by*. This means that we need to:
677 // (1) Find the line position of the current line.
678 // (2) Find the line position of pos.
679 // (3) Scroll by (pos - current).
680 // For now, ignore the horizontal scrolling.
681
682 // Is this where scrolling is relative to - the line containing the caret?
683 // Or is the first visible line??? Try first visible line.
684// int currentLineLineNo1 = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)-1, (LPARAM)0L);
685
686 int currentLineLineNo = (int)SendMessage(hWnd, EM_GETFIRSTVISIBLELINE, (WPARAM)0, (LPARAM)0L);
687
688 int specifiedLineLineNo = (int)SendMessage(hWnd, EM_LINEFROMCHAR, (WPARAM)pos, (LPARAM)0L);
39136494 689
2bda0e17
KB
690 int linesToScroll = specifiedLineLineNo - currentLineLineNo;
691
2bda0e17 692 if (linesToScroll != 0)
de4d7713 693 (void)SendMessage(hWnd, EM_LINESCROLL, (WPARAM)0, (LPARAM)linesToScroll);
2bda0e17
KB
694}
695
debe6624 696int wxTextCtrl::GetLineLength(long lineNo) const
2bda0e17
KB
697{
698 long charIndex = XYToPosition(0, lineNo);
4438caf4 699 int len = (int)SendMessage(GetHwnd(), EM_LINELENGTH, charIndex, 0);
2bda0e17
KB
700 return len;
701}
702
debe6624 703wxString wxTextCtrl::GetLineText(long lineNo) const
2bda0e17 704{
a1b82138 705 size_t len = (size_t)GetLineLength(lineNo) + 1;
4438caf4
VZ
706 char *buf = (char *)malloc(len);
707 *(WORD *)buf = len;
708 int noChars = (int)SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf);
709 buf[noChars] = 0;
710
711 wxString str(buf);
712
713 free(buf);
714
715 return str;
2bda0e17
KB
716}
717
a1b82138 718// ----------------------------------------------------------------------------
ca8b28f2 719// Undo/redo
a1b82138
VZ
720// ----------------------------------------------------------------------------
721
ca8b28f2
JS
722void wxTextCtrl::Undo()
723{
724 if (CanUndo())
725 {
789295bf 726 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
727 }
728}
729
730void wxTextCtrl::Redo()
731{
732 if (CanRedo())
733 {
734 // Same as Undo, since Undo undoes the undo, i.e. a redo.
789295bf 735 ::SendMessage(GetHwnd(), EM_UNDO, 0, 0);
ca8b28f2
JS
736 }
737}
738
739bool wxTextCtrl::CanUndo() const
740{
789295bf 741 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
742}
743
744bool wxTextCtrl::CanRedo() const
745{
789295bf 746 return (::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0);
ca8b28f2
JS
747}
748
a1b82138
VZ
749// ----------------------------------------------------------------------------
750// implemenation details
751// ----------------------------------------------------------------------------
39136494 752
2bda0e17
KB
753void wxTextCtrl::Command(wxCommandEvent & event)
754{
a1b82138
VZ
755 SetValue(event.GetString());
756 ProcessCommand (event);
2bda0e17
KB
757}
758
759void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
760{
a1b82138
VZ
761 // By default, load the first file into the text window.
762 if (event.GetNumberOfFiles() > 0)
763 {
764 LoadFile(event.GetFiles()[0]);
765 }
2bda0e17
KB
766}
767
2bda0e17
KB
768void wxTextCtrl::OnChar(wxKeyEvent& event)
769{
42e69d6b 770 switch ( event.KeyCode() )
cd471848 771 {
cd471848 772 case WXK_RETURN:
39136494 773 if ( !(m_windowStyle & wxTE_MULTILINE) )
cd471848
VZ
774 {
775 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
776 event.SetEventObject( this );
777 if ( GetEventHandler()->ProcessEvent(event) )
778 return;
779 }
5fb9fcfc
VZ
780 //else: multiline controls need Enter for themselves
781
782 break;
4d91c1d1 783
cd471848 784 case WXK_TAB:
319fefa9
VZ
785 // always produce navigation event - even if we process TAB
786 // ourselves the fact that we got here means that the user code
787 // decided to skip processing of this TAB - probably to let it
788 // do its default job.
5fb9fcfc
VZ
789 //
790 // NB: Notice that Ctrl-Tab is handled elsewhere and Alt-Tab is
791 // handled by Windows
cd471848 792 {
5fb9fcfc
VZ
793 wxNavigationKeyEvent eventNav;
794 eventNav.SetDirection(!event.ShiftDown());
795 eventNav.SetWindowChange(FALSE);
796 eventNav.SetEventObject(this);
39136494 797
5fb9fcfc 798 if ( GetEventHandler()->ProcessEvent(eventNav) )
cd471848
VZ
799 return;
800 }
341c92a8 801 break;
4d91c1d1
VZ
802
803 default:
804 event.Skip();
39136494 805 return;
cd471848 806 }
39136494 807
cd471848
VZ
808 // don't just call event.Skip() because this will cause TABs and ENTERs
809 // be passed upwards and we don't always want this - instead process it
810 // right here
42e69d6b
VZ
811
812 // FIXME
813 event.Skip();
2bda0e17
KB
814}
815
debe6624 816bool wxTextCtrl::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 817{
789295bf
VZ
818 switch (param)
819 {
820 case EN_SETFOCUS:
821 case EN_KILLFOCUS:
822 {
823 wxFocusEvent event(param == EN_KILLFOCUS ? wxEVT_KILL_FOCUS
824 : wxEVT_SET_FOCUS,
825 m_windowId);
826 event.SetEventObject( this );
827 GetEventHandler()->ProcessEvent(event);
828 }
829 break;
ae29de83 830
789295bf
VZ
831 case EN_CHANGE:
832 {
833 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_windowId);
834 wxString val(GetValue());
835 if ( !val.IsNull() )
836 event.m_commandString = WXSTRINGCAST val;
837 event.SetEventObject( this );
838 ProcessCommand(event);
839 }
840 break;
2bda0e17 841
789295bf
VZ
842 case EN_ERRSPACE:
843 // the text size limit has been hit - increase it
844 AdjustSpaceLimit();
845 break;
846
847 // the other notification messages are not processed
848 case EN_UPDATE:
849 case EN_MAXTEXT:
850 case EN_HSCROLL:
851 case EN_VSCROLL:
852 default:
853 return FALSE;
854 }
855
856 // processed
857 return TRUE;
2bda0e17
KB
858}
859
789295bf
VZ
860void wxTextCtrl::AdjustSpaceLimit()
861{
25889d3c 862#ifndef __WIN16__
789295bf
VZ
863 unsigned int len = ::GetWindowTextLength(GetHwnd()),
864 limit = ::SendMessage(GetHwnd(), EM_GETLIMITTEXT, 0, 0);
865 if ( len > limit )
866 {
867 limit = len + 0x8000; // 32Kb
868
5ea105e0 869#if wxUSE_RICHEDIT
789295bf 870 if ( m_isRich || limit > 0xffff )
5ea105e0
RR
871#else
872 if ( limit > 0xffff )
873#endif
789295bf
VZ
874 ::SendMessage(GetHwnd(), EM_LIMITTEXT, 0, limit);
875 else
876 ::SendMessage(GetHwnd(), EM_LIMITTEXT, limit, 0);
877 }
25889d3c 878#endif
789295bf 879}
2bda0e17 880
a1b82138 881bool wxTextCtrl::AcceptsFocus() const
2bda0e17 882{
a1b82138
VZ
883 // we don't want focus if we can't be edited
884 return IsEditable() && wxControl::AcceptsFocus();
885}
c085e333 886
f68586e5 887wxSize wxTextCtrl::DoGetBestSize() const
a1b82138
VZ
888{
889 int cx, cy;
890 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
891
892 int wText = DEFAULT_ITEM_WIDTH;
893
894 int hText = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy);
895 if ( m_windowStyle & wxTE_MULTILINE )
896 {
897 hText *= wxMin(GetNumberOfLines(), 5);
898 }
899 //else: for single line control everything is ok
900
901 return wxSize(wText, hText);
2bda0e17 902}
a1b82138
VZ
903
904// ----------------------------------------------------------------------------
905// standard handlers for standard edit menu events
906// ----------------------------------------------------------------------------
2bda0e17 907
e702ff0f
JS
908void wxTextCtrl::OnCut(wxCommandEvent& event)
909{
910 Cut();
911}
912
913void wxTextCtrl::OnCopy(wxCommandEvent& event)
914{
915 Copy();
916}
917
918void wxTextCtrl::OnPaste(wxCommandEvent& event)
919{
920 Paste();
921}
922
923void wxTextCtrl::OnUndo(wxCommandEvent& event)
924{
925 Undo();
926}
927
928void wxTextCtrl::OnRedo(wxCommandEvent& event)
929{
930 Redo();
931}
932
933void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
934{
935 event.Enable( CanCut() );
936}
937
938void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
939{
940 event.Enable( CanCopy() );
941}
942
943void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
944{
945 event.Enable( CanPaste() );
946}
947
948void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
949{
950 event.Enable( CanUndo() );
951}
952
953void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
954{
955 event.Enable( CanRedo() );
956}
957